您的位置:首页 > 数据库

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

2011-01-27 21:39 302 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: