Click to See Complete Forum and Search --> : SQL Stored Procedure Problem


Lynne
May 16th, 2001, 09:11 AM
Hi
I have a question regarding SLQ variables. I am setting the variable through a cursor. When I check the syntax of this SQL, it says that I need to declare @TableName on the line that says
select @eid = eid from @tableName . Is it not possible to do this? I know that I've used such variable in a where clause. Is it because I am using it in a from clause? Do I need some other kind of syntax to do this?

declare @elementClassId int,
@eid eid,
@className varchar(32),
@tableName varchar(32),
@aid eid,
@attributeName varchar(32)

declare classCursor cursor for
select classId, TableName from tmbElementClasses where classAttributes = 3
order by TableName

open classCursor
fetch classCursor into @elementClassId, @tableName
while (@@fetch_status = 0)
BEGIN

if( @elementClassId is not NULL and @tableName is not NULL )
begin
select @eid = eid from @tableName
end
fetch classCursor into @elementClassId, @tableName

END
close classCursor
deallocate classCursor

Thanks in advance

Clearcode
May 16th, 2001, 09:25 AM
It is not possible to substitute a variable for a table or view name in a select statement, sorry.

The best workaround is to have:

If @tablename = TCLMDETS
Select @eid = eid FROM TCLMDETS
Else
Select @eid = eid FROM TCLMTRXN

or as many IFs as you have tables.

HTH,
D

-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com