Ref:http://www.cnblogs.com/Gavinzhao/archive/2010/07/14/1777644.html

declare @Id varchar(100),@name varchar(10)
declare ccursor cursor for select MAXID, name from test.dbo.A
open ccursor
FETCH NEXT FROM ccursor INTO @Id,@name

while @@FETCH_STATUS=0
begin
select @Id,@name
FETCH NEXT FROM ccursor INTO @Id,@name
end

close ccursor --关闭cursor

deallocate ccursor --删除cursor

游标一般格式:
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 游标名称 (删除游标)

代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->例子:
/*
功能:数据库表格tbl_users数据
deptid userid username
100 a
101 b
102 c
要求用一个sql语句输出下面结果
deptid username
ab
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

Cursor use的更多相关文章

  1. 自定义鼠标光标cursor

    通过css属性 Cursor:url()自定义鼠标光标. {cursor:url('图标路径'),default;} url是自定义鼠标图标路径 default指的是定义默认的光标(通常是一个箭头), ...

  2. 苹果手机不支持click文字 需要添加 cursor:pointer 才能 识别可以点击

    给一个div 绑定一个 click事件,  苹果手机会识别不了,必须添加一个 cursor:pointer 才能 识别可以点击.安卓正常识别.

  3. java.lang.IllegalStateException:Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx...}: java.lang.IllegalSta ...

  4. MySQL:procedure, function, cursor,handler

    Procedure & Function Procedure 语法: CREATE [DEFINER = { user | CURRENT_USER }] PROCEDURE sp_name ...

  5. Sql Cursor example

    USE [EUC]GO/****** Object:  StoredProcedure [dbo].[SP_SME_QueryAuditLog]    Script Date: 02/05/2015 ...

  6. [Android Pro] 完美Android Cursor使用例子(Android数据库操作)

    reference to : http://www.ablanxue.com/prone_10575_1.html 完美 Android Cursor使用例子(Android数据库操作),Androi ...

  7. 游标cursor

    if exists(select * from sys.objects where name='info_one') drop table info_one go create table info_ ...

  8. Android笔记——关于Cursor类的介绍

    使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android 中使用 Cursor 的理解. 关于 Cursor 在你理解和使用 Android Cursor 的时候你 ...

  9. SQL游标(cursor)详细说明及内部循环使用示例

    游标 游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果.每个游标区都有一个名字,用户可以用SQL语句逐一从游标中获取记录,并赋给主变量,交由主语言进一步处理. 游标是处理 ...

  10. cursor.MySQLCursorDict Class

    5.9.6.4 cursor.MySQLCursorDict Class The MySQLCursorDict class inherits from MySQLCursor. This class ...

随机推荐

  1. java中的List记录是否完全匹配方法

    今天要说的是给List分组,然后用Map来封装,可能你看了以后还是有一些模糊. 先看一下项目结构图: User类是一个VO类,主要逻辑还是在MapTestBak上面. 运行效果: 原理图: 1.在st ...

  2. java代码过滤emoji表情

    可以新建一个过滤器的类,在类中书写如下代码: public static String filterEmoji(String source) {           if(source != null ...

  3. JS相关环境搭建:Nodejs、karma测试框架、jsDuck、Express

    第一章:压缩js(nodejs,uglify) 第一步:安装nodejs环境 直接下载http://www.nodejs.org/download/ 下载完成后直接下一步下一步即可,完了我们就具有no ...

  4. X-UA-Compatible/IE=EmulateIE7/IE=7

    1.<meta http-equiv="X-UA-Compatible" content="IE=5" /> 像是使用了 Windows Inter ...

  5. JSch - Java实现的SFTP(文件上传详解篇)

    JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到 ...

  6. [LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  7. WinForm 窗体属性

    WinForm - C/S 客户端     B/S 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 ...

  8. C++复制对象时勿忘每一部分

    现看这样一个程序: void logCall(const string& funcname) //标记记录 { cout <<funcname <<endl; } cl ...

  9. 【HDU2196 Computer】经典树形dp

    http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:有n台电脑相连,让你求每台电脑与离它最远的那台电脑的距离. 思路:两遍搜索即可,第一遍从上到下,第二 ...

  10. openfire配置MSSQL说明(数据库设置)

    1.进入“SQL Server 配置管理器(SQL Server Configuration Manager)”,在左边窗口选择“SQL Server 2005网络配置”下面的分支“MSSQLServ ...