select from various tables using a variable | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

select from various tables using a variable

I have a cursor which will return a number of databases Within this cursor, I want to perform a select statement against the result set of the cursor. I am having trouble reference the table name as a various within the cursor. Any assistance would be appreciated.
declare db_name varchar(50) declare database_cursor CURSOR for
select name from master..sysdatabases open database_cursor
fetch next from database_cursor
into @db_name
while @@FETCH_STATUS=0
declare object_cursor CURSOR for
select name from @db_name..sysobjects this declare statement is where I am having the problem.
Heya,<br /><br />You will have to build your SQL string and run it using: EXEC (@SQLString).<br />I dont really have too much experience with this, but have a look at Dynamic SQL and EXECUTE in BOL <img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ /><br /><br />Ben
Cursor are evil. Cursor AND dynamic SQL is even worse. Maybe we can get to workaround it when you explain what you’re after. In the meantime read this: