sql 游标简单使用(判断临时表是否存在)


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

posted on 2011-01-27 21:39  jianshaohui  阅读(599)  评论(0编辑  收藏  举报

导航