INF: Object Names Cannot be Variables in TRANSACT-SQLID: Q65466
|
Because object resolution occurs at compile time, TRANSACT-SQL variables (@variable) cannot be used in place of object names. For example, TRANSACT-SQL does not allow variable database names.
However, you can use any of the following three alternatives to
simulate variable table names:
...
char table[40];
char *storid = "6380";
printf ("Please Enter the table to extract: ");
gets(table);
dbfcmd(dbproc, "select * from %s ", table);
dbfcmd(dbproc, "where stor_id = '%s' ", storid);
dbsqlexec (dbproc);
while (dbresults(dbproc)) != NO_MORE_RESULTS)
dbprrow(dbproc);
...
@echo off
set db=%1
set tb=%2
set data=%3
echo /* SQL Input file (getdat %db% %tb% %data%) */> isql.in
echo use %db% >> isql.in
echo go >> isql.in
echo select * from %tb% >> isql.in
echo where title_id = '%data%' >> isql.in
echo go >> isql.in
:*
:* Select from table
:*
isql /U sa /P /S SERVERNAME /n /e /p <isql.in >getdat.out
create procedure getdata @dbname varchar (30),
@tabname varchar (20),
@dataname varchar(24)
as
if @dbname = "pubs"
if @tabname = "Titles" . . .
select * from pubs..titles
where title_id = @dataname
if @tabname = "titleauthor"
select * from pubs..titleauthor
where title_id = @dataname
if @dbname = "tst"
print "oops."
Keywords : kbprg SSrvGen SSrvTrans SSrvServer
Version : 4.2
Platform : OS/2
Issue type :
Last Reviewed: March 9, 1999