Showing posts with label generation. Show all posts
Showing posts with label generation. Show all posts

Sunday, March 11, 2012

Dynamic generation of table name

I have a database that has some of its tables created at run-time, i.e., a table name is computed by a program and then the table is created, and the data - written to it. My program will have no trouble finding those tables but the end users will want to run queries using a DBMS client of their choice. They may simply want us to write them some predefined queries that they can execute in MSQuery to build Excel spreadsheets, or something like that.
My question is: is it possible to put in the FROM clause of a SELECT statement anything other than a string describing the name of a table in the database, and more specifically, a sub-query? Let's say I have a table with a known name that contains two columns, and I want to use the concatenation of the contents of those two columns to build the names of some tables, which will then be used in the FROM clause of another SELECT statement. Is that possible and how can I do that? Thanks in advance!
KamenFor end-users: NO, for Developers you can use dynamic SQL. :shocked:
Maybe for the end-users you could create a synonym for these tables, for example: every time you create a new table, you could drop old synonym and create new pointing to new table. ;)|||Bummer... That's what I thought, looking at the SQL documentation. I guess they can always use some "programmable" client, such as MS Access. Thanks, anyway.
Kamen

Sunday, February 26, 2012

Dynamic Cursor Generation..

Hi Everybody,
I have a probs with dynamic generation.
I am writing the probs
======================================
create proc test
as
declare @.query varchar(500)
set @.query = 'select * from table'
--------------
declare mycur Cursor for Select * from table |
open mycur |
--------------
but instate of above block how can I dynamically generate this query?
------------
declare mycur Cursor for exec (@.query) |
------------
Or tell me the way.
Regards
Arijit Chatterjeehi, the problem was not clear what u want to do dynamiclly.
if u want to ftech rows thru proc, u can code like

create proc test
as
use db
exec(select * from table)

this might work

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||On 4 Nov 2003 21:04:27 -0800, arijitchatterjee123@.yahoo.co.in (Arijit
Chatterjee) wrote:
>but instate of above block how can I dynamically generate this query?
>------------
>declare mycur Cursor for exec (@.query) |
>------------

The way I've done stuff like this in the past is to stuff the results
into a temp table and run a cursor off that.

Example:
declare @.SQLstring varchar(100)
create table #foo (foovar int)

select @.SQLString = 'select distinct foovar from footable'

insert into #foo exec(@.SQLString)

declare foo_cursor for select distinct foovar from #foo
etc.

Obviously you really want to do this type of stuff when there is no
other way around it.

>Or tell me the way.
>Regards
>Arijit Chatterjee