把结果集放到临时表中的示例代码(使用游标)
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 --删除临时表
--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 --删除临时表
相关文章推荐
- mysql 存储过程示例,有参有返回值,使用到了判断,循环,游标,临时表,事务
- Mysql存储过程循环内嵌套使用游标示例代码
- 编写存储过程返回的结果集在游标中使用java代码调用
- Mysql存储过程循环内嵌套使用游标示例代码
- sql循环嵌套、游标使用、循环结果集示例
- C#中使用事件(代码示例)
- 示例 - 25行代码等价实现 - 借助Nodejs在服务端使用jQuery采集17173游戏排行信息
- 游标、type使用示例
- postgresql对于各种游标的使用示例
- 用JavaScript实现使用鼠标画线的示例代码
- C#使用Mutex实现系统范围内单实例运行的示例代码
- ThreadLocal的使用与代码示例(译)
- Cookie的使用之保存与获取代码示例
- 关于oracle with table as 创建临时表的用法示例以及使用with as 的优点
- Django原生sql也能使用Paginator分页的示例代码
- 使用HttpURLConnection抓网页代码示例
- php示例代码之使用list函数和mysql_fetch_row函数
- java加解密RSA使用方法代码示例
- 使用electron实现百度网盘悬浮窗口功能的示例代码