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. 关于android app两次点击返回键退出的处理

    现在的android app在开发时,引入了两次点击返回键退出app的设计 为了避免用户误触,这个设计很人性化 中文网上社区有些同学贴了一些实现的例子,我觉得不是很好 代码如下 public bool ...

  2. JSP中使用的模式——JSP+Servlet+JavaBean

    上一篇博文写到模式一:JSP+JavaBean 链接地址:http://wxmimperio.coding.io/?p=155 JSP中两种模式的总结 链接地址:http://wxmimperio.c ...

  3. (转载)Convolutional Neural Networks卷积神经网络

    Convolutional Neural Networks卷积神经网络 Contents 一:前导 Back Propagation反向传播算法 网络结构 学习算法 二:Convolutional N ...

  4. hadoop的一些重要配置参数

    hadoop集群管理内存设置 Mapreduce内存使用设置 hadoop job重要性能参数

  5. [Quote]Creating basic Excel workbook with Open XML

    Creating basic Excel workbook with Open XML [Quote from]http://www.codeproject.com/Articles/371203/C ...

  6. Watch gcc at ubuntu 12,See ELF file header

    first write article at my ubuntu 12. ELF is very important file format.

  7. .classpath 和.project文件含义

    .classpath文件是在建立eclipse工程时创建的描述工程配置情况的文件,包括:  * 源码路径  * 编译结果的输出路径  * 所使用的外部库的路径 -----------------下面是 ...

  8. Linux学习笔记23——取消线程

    一 相关函数 1 发送终止信号 #include <pthread.h> int pthread_cancel(pthread_t thread); 2 设置取消状态 #include & ...

  9. 一个简单的GI--Reflective Shadow Maps

    Reflective Shadow Maps是一种简单的GI,虽然名字带有Shadow Maps,但是和Shadow Maps没有什么关系,是在screen space的一种方法,不像光线追踪和辐射度方 ...

  10. ACM1230_火星A+B(进位的运算)

    //只要看懂火星A+B的进位关系就好了 #include<stdio.h> ]={,,,,,,,,,,,,,,,,,,,,,,,,,}; int main() { ],b[],sum[]; ...