Tuesday, March 27, 2012

Select a table dynamically in a database using passing a parameter tablename

declare @tablename varchar(50)

declare @cursor cursor

set @cursor=CURSOr for

select table_Name from information_schema.tables 

where table_type='base table'

open @cursor

fetch next

from @cursor into @tablename

while @@FETCH_STATUS=0

begin

declare @sql varchar(500)

set @sql = 'select * from ' + @tablename

exec (@sql)

fetch next

from @cursor into @tablename

end

close @cursor

deallocate @cursor


No comments:

Post a Comment