您的位置:首页 > 编程语言

把结果集放到临时表中的示例代码(使用游标)

2013-06-18 23:06 351 查看
declare @ActionDesc nvarchar(max) -- 一个策略字段:(目的是为了确认唯一的一行数据内容),联想到Group by的用法,

--group by前面的一个字段是为了统计,后面是要分组的字段,然后得到要统计的字段(面向对象思想)

declare @ID uniqueidentifier

declare cursor1 cursor for --定义游标

select ID,ActionDesc from T_OperatorLog t --使用游标的对象(跟据需要填入select文)

open cursor1 --打开游标

fetch next from cursor1 into @ID,@ActionDesc --将游标向下移行,获取的数据放入之前定义的变量@id,@name中

while @@fetch_status=0 --判断是否成功获取数据

begin

if(@ActionDesc='描述3')

update T_OperatorLog set ActionDesc='修改的描述' where
ID=@ID --进行相应处理(跟据需要填入SQL文)

fetch next from cursor1 into @ID,@ActionDesc --将游标向下移行

end

select * into #temp1 from T_OperatorLog where ActionDesc='修改的描述' --把上面游标修改好的结果集放入到临时表中

close cursor1 --关闭游标

deallocate cursor1 --撤销游标

select * from #temp1 --从临时表中取到刚才得到的结果集

drop table #temp1 --删除临时表
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: