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 基本用法的更多相关文章

  1. sql Cursor的用法

    table1结构如下 id int name ) declare @id int ) declare cursor1 cursor for --定义游标cursor1 select * from ta ...

  2. 游标SQL Cursor 基本用法

    http://www.cnblogs.com/Gavinzhao/archive/2010/07/14/1777644.html 1 table1结构如下 2 id    int 3 name  va ...

  3. SQL Cursor 基本用法[用两次FETCH NEXT FROM INTO语句?]

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  4. sql的游标用法举例(Cursor)

    sql的游标用法举例 ), ) Declare authors_cursor Cursor For Select Name,TrueName From Account Open authors_cur ...

  5. SQL语句---nvl 用法

    SQL语句---nvl 用法   一NVL函数是一个空值转换函数 NVL(表达式1,表达式2) 如果表达式1为空值,NVL返回值为表达式2的值,否则返回表达式1的值. 该函数的目的是把一个空值(nul ...

  6. SQL 语句日期用法及函数

    SQL 语句日期用法及函数 --DAY().MONTH().YEAR()——返回指定日期的天数.月数.年数:select day(cl_s_time) as '日' from class  --返回天 ...

  7. SQL 中ROLLUP 用法

    SQL 中ROLLUP 用法 ROLLUP 运算符生成的结果集类似于 CUBE 运算符生成的结果集. 下面是 CUBE 和 ROLLUP 之间的具体区别: CUBE 生成的结果集显示了所选列中值的所有 ...

  8. css cursor url用法格式详解

    css cursor url用法格式:css:{cursor:url('图标路径'),auto;} //IE,FF,chrome浏览器都可以 实例代码:html{cursor: url("h ...

  9. 标准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 ...

随机推荐

  1. spm使用之三spm应用实例

    spm 的init实际上是调用了grunt这个工具来实现一些交互式的提问和数据的获取. 看看npm就知道, npm有个命令叫init, 就是一样的交互式提问获取你要创建的nodejs的模块信息. sp ...

  2. 实现pushViewController:animated:的不同页面转换特效

    1. 首先要明确的是,不使用pushViewController的默认动画,所以在调用这个函数时,要将animated设置为NO.2. 使用普通的来CATransition实现转换效果,代码如下:CA ...

  3. dpkg error

    在ubuntu乱搞,突然出现错误 dpkg: error: cannot read info directory: No such file or directory E: Sub-process / ...

  4. bzoj3261

    xor有一个很重要的性质就是A xor B xor B=A所以这道题求[l,r]中p,使a[p] xor a[p+1] xor ... xor a[N] xor x 最大就是=最大化a[1] xor ...

  5. WIA

    一台扫描仪,实际上就是一个Device对象,因此,我们可以通过DeviceManager来“获取”这台设备的“引用”,然后通过得到的Device对象,执行相应的扫描工作.从而跳过了使用ShowAcqu ...

  6. 利用Trie树对字符串集合进行排序并计算特征值

    该算法用于将一组乱序的字符串反序列化到一个Trie树中,这个过程即可视为对字符串进行了一次排序. 还可以通过调用 GetFeatureString 将该 Trie 树重新序列化. #include & ...

  7. zoj 3659 Conquer a New Region(并查集)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...

  8. Google辅助类软件

    本博文的主要内容有 .Google辅助类软件的介绍 .重点首推!  Google软件精选管理器 1.Google辅助类软件的介绍 1.  Google软件精选管理器的下载和安装使用 2.  Googl ...

  9. 在SQL Server 2012中如何使用分组集

    作者:Itzik Ben-Gan  翻译:张洪举 此文摘自作者的<Microsoft SQL Server 2012 T-SQL基础>. 分组集就是你据以分组的一个属性集.传统上,SQL中 ...

  10. oracle从客户端到sql语句追踪

    这两天看小布老师的视频学习了一下从客户端到oracle数据库发送执行的SQL语句的跟踪,整理一下笔记. 需要用到的命令:netstat oracle端要用到的四个视图为: V$session:当前有多 ...