You will need to use dynamic SQL. Create an SQL string with the query you want to execute, and then call exec(@sql)
Full example:
declare cur cursor for select tbl_names from tbl1 declare @sql varchar(100), @tbl varchar(100) open cur fetch cur into @tbl while @@FETCH_STATUS = 0 begin set @sql = 'select * from ' + @tbl exec(@sql) fetch cur into @tbl end close cur deallocate cur
Sean coetzee
source share