现在的位置: 首页 > 综合 > 正文

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

2012年11月28日 ⁄ 综合 ⁄ 共 481字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.