SQL Cursor 基本用法
1 table1结构如下 2 id int 3 name varchar(50) 4 5 declare @id int 6 declare @name varchar(50) 7 declare cursor1 cursor for --定义游标cursor1 8 select * from table1 --使用游标的对象(跟据需要填入select文) 9 open cursor1 --打开游标 10 11 fetch next from cursor1 into @id,@name --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中 12 13 while @@fetch_status=0 --判断是否成功获取数据 14 begin 15 update table1 set name=name+'1' 16 where id=@id --进行相应处理(跟据需要填入SQL文) 17 18 fetch next from cursor1 into @id,@name --将游标向下移1行 19 end 20 21 close cursor1 --关闭游标 22 deallocate cursor1
游标一般格式:
DECLARE 游标名称 CURSOR FOR SELECT 字段1,字段2,字段3,... FROM 表名 WHERE ...
OPEN 游标名称
FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,...
WHILE @@FETCH_STATUS=0
BEGIN
SQL语句执行过程... ...
FETCH NEXT FROM 游标名称 INTO 变量名1,变量名2,变量名3,...
END
CLOSE 游标名称
DEALLOCATE 游标名称 (删除游标)
例子: /* 功能:数据库表格tbl_users数据 deptid userid username 1 100 a 1 101 b 2 102 c 要求用一个sql语句输出下面结果 deptid username 1 ab 2 c [要求用游标实现设计: OK_008 时间: 2006-05 备注:无 */ create table #Temp1(deptid int,userid int,username varchar(20)) --待测试的数据表 create table #Temp2(deptid int,username varchar(20)) --结果表 --先把一些待测试的数据插入到待测试表#Temp1中 insert into #Temp1 select 1,100,'a' union all select 1,101,'b' union all select 1,131,'d' union all select 1,201,'f' union all select 2,302,'c' union all select 2,202,'a' union all select 2,221,'e' union all select 3,102,'y' union all select 3,302,'e' union all select 3,121,'t' -- declare @deptid int,@username varchar(20) --定义游标 declare Select_cursor cursor for select deptid,username from #Temp1 open Select_cursor fetch next from Select_cursor into @deptid,@username --提取操作的列数据放到局部变量中 while @@fetch_status=0 --返回被 FETCH 语句执行的最后游标的状态 /* @@FETCH_STATUS =0 FETCH 语句成功 @@FETCH_STATUS =-1 FETCH 语句失败或此行不在结果集中 @@FETCH_STATUS =-2 被提取的行不存在 */ begin --当表#Temp2列deptid存在相同的数据时,就直接在列username上追加@username值 if(exists(select * from #Temp2 where deptid=@deptid )) update #Temp2 set username=username +@username where deptid=@deptid else --插入新数据 insert into #Temp2 select @deptid,@username fetch next from Select_cursor into @deptid,@username end close Select_cursor deallocate Select_cursor select * from #Temp2 --测试结果 Drop table #Temp1,#Temp2
SQL Cursor 基本用法的更多相关文章
- sql Cursor的用法
table1结构如下 id int name ) declare @id int ) declare cursor1 cursor for --定义游标cursor1 select * from ta ...
- 游标SQL Cursor 基本用法
http://www.cnblogs.com/Gavinzhao/archive/2010/07/14/1777644.html 1 table1结构如下 2 id int 3 name va ...
- SQL Cursor 基本用法[用两次FETCH NEXT FROM INTO语句?]
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- sql的游标用法举例(Cursor)
sql的游标用法举例 ), ) Declare authors_cursor Cursor For Select Name,TrueName From Account Open authors_cur ...
- SQL语句---nvl 用法
SQL语句---nvl 用法 一NVL函数是一个空值转换函数 NVL(表达式1,表达式2) 如果表达式1为空值,NVL返回值为表达式2的值,否则返回表达式1的值. 该函数的目的是把一个空值(nul ...
- SQL 语句日期用法及函数
SQL 语句日期用法及函数 --DAY().MONTH().YEAR()——返回指定日期的天数.月数.年数:select day(cl_s_time) as '日' from class --返回天 ...
- SQL 中ROLLUP 用法
SQL 中ROLLUP 用法 ROLLUP 运算符生成的结果集类似于 CUBE 运算符生成的结果集. 下面是 CUBE 和 ROLLUP 之间的具体区别: CUBE 生成的结果集显示了所选列中值的所有 ...
- css cursor url用法格式详解
css cursor url用法格式:css:{cursor:url('图标路径'),auto;} //IE,FF,chrome浏览器都可以 实例代码:html{cursor: url("h ...
- 标准SQL语言的用法
原文链接:http://www.ifyao.com/2015/05/18/%E6%A0%87%E5%87%86%E7%9A%84sql%E8%AF%AD%E8%A8%80%E4%BD%BF%E7%94 ...
随机推荐
- HTML+JS版本的俄罗斯方块
<!doctype html><html><head></head><body> <div id="box" st ...
- nutch-1.7 编译
转载自:http://peigang.iteye.com/blog/1563288 从nutch-.3开始 本地抓取(单击) 和 分布式抓取(集群)所使用的配置文件和命令单独分开. 资源:下载地址:h ...
- App Store内购
一.In App Purchase概览 Store Kit代表App和App Store之间进行通信.程序将从App Store接收那些你想要提供的产品的信息,并将它们显示出来供用户购买.当用户需要购 ...
- Angular 2 npm start 报错
首先, index.html 和styles.css是和app目录平级的, 不要扔到里面去, 否则会404 确认配置文件齐全, 路径都正确之后 npm start What?! 照着快速起步也会弄错吗 ...
- Android如何正确的保存文件
在Android 官方开发文档中有一篇文档来介绍如何保存应用的数据,但笔者用过很多程序(从知名的到不知名的)处理的都不是很完美,或者 没有按照Android 开发团队建议的方式去保存他们应用的数据.当 ...
- bzoj 1923 [Sdoi2010]外星千足虫(高斯消元+bitset)
1923: [Sdoi2010]外星千足虫 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 634 Solved: 397[Submit][Status ...
- typedef用法小结
typedef用法小结- - 注意:本文转自网络,版权归原作者所有. typedef typedef用法小结- - 这两天在看程序的时候,发现很多地方都用到typedef,在结构体定义,还有一些数组等 ...
- Using QEMU for Embedded Systems Development
http://www.opensourceforu.com/2011/06/qemu-for-embedded-systems-development-part-1/ http://www.opens ...
- 中科院NLPIR中文分词java版
中科院NLPIR中文分词java版 中科院NLPIR中文分词java版
- hdu 2066 一个人的旅行(最短路问题)
最短路································· 类似的问题还有好多不会!慢慢学吧!!!!. 进步,哪怕每天一点也行! (恋爱不是小事,确实小事的积累!(听着酷狗音乐台说的,很 ...