Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Using Variables for Table Names in Select Statements

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-02-05 : 16:24:19
Raymond writes "Here's what I am trying to do:

USE SOMEDATABASE
DECLARE @tablename nvarchar(100)
DECLARE @tabletobequeried nvarchar(100)
DECLARE tablecursor CURSOR FOR select distinct name from sysobjects where xtype = 'u'
OPEN tablecursor
FETCH NEXT FROM tablecursor into @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
set @tabletobequeried = @tablename

PRINT @tablename -- To prove to my mind that it's sticking.
PRINT @tabletobequeried -- To prove I am not insane!!!
SELECT * from @tabletobequeried -- Where insanity begins!

FETCH NEXT FROM tablecursor into @tablename
END
CLOSE tablecursor
DEALLOCATE tablecursor

What am I doing wrong here?
"
   

- Advertisement -