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. 《深入剖析Tomcat》阅读(一)

    第一章 一个简单的Web服务器 该应用程序仅接受位于指定目录的静态资源的请求,如HTML文件和图像文件.它也可以将传入的HTTP请求字节流显示到控制台上.但是,它并不发送任何头信息到浏览器,如日期或者 ...

  2. android ListView 多次调用 getView方法

    <ListView            android:layout_width="match_parent"            android:layout_heig ...

  3. 根据http协议传送数据

    发送的内容: [50 4f 53 54 20 2f 64 65 78 2f 66 69 72 65 70 6f 77 65 72 20 48 54 54 50 2f 31 2e 31 0d 0a 43 ...

  4. 写个自动安装JDK的shell脚本

    #!/bin/bash ################################################# # # INSTALL JDK AUTOMATICALLY # # auth ...

  5. 如何对 Android 库进行依赖管理?

    Android 开发人员为项目选择库的时候,考虑的因素不仅仅是功能.可用性.性能.文档丰富度和技术支持情况.他们还关心库的大小,以及要添加的方法数量.因为项目越大,依赖也越多,要把应用的方法数量控制在 ...

  6. Altium Designer 定义板子外框

    Altium Designer 提供多种定义板子外形的方法. 第一种方法,在Files 面板(在界面下面System菜单条中查找)中选择PCB Templates命令.在这个界面下您可以选择符合您设计 ...

  7. 代理Delegate的小应用(代理日期控件和下拉框)

    前言 在平时关于表格一类的的控件使用中,不可避免需要修改每个Item的值,通过在Item中嵌入不同的控件对编辑的内容进行限定,然而在表格的Item中插入的控件始终显示,当表格中item项很多的时候,会 ...

  8. mysql优化21条经验(转)

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序 员需要去关注的事情.当我们去设计数据库表结构,对操作数 ...

  9. Java Random随机种子

    第一种情况 Random rand = new Random(47); for(int i=0;i<10;i++) System.out.println(rand.nextInt(100)); ...

  10. java jdbc使用配置文件连接数据库:

    java jdbc使用配置文件连接数据库: 创建后缀名为:.properties的文件,文件内容包括,数据库驱动.连接的数据库地址.用户名.密码…… 以Mysql为例创建config.properti ...