IF OBJECT_ID('tempdb..#tmp') IS NOT NULL
DROP TABLE #tmp
select * into #tmp from (select ('select * from ' +name) as 'name' from sysobjects where xtype='U') q
declare @id nvarchar(200) --定义变量来保存ID号
declare mycursor cursor for select * from #tmp --为所获得的数据集指定游标
open mycursor --打开游标
fetch next from mycursor into @id --开始抓第一条数据
while(@@fetch_status=0) --如果数据集里一直有数据
begin
exec (@id)
fetch next from mycursor into @id --跳到下一条数据
end
close mycursor --关闭游标
deallocate mycursor --删除游标
--drop table #tmp
转载于:https://www.cnblogs.com/jianshao810/archive/2011/01/27/1946476.html