深入SQL Cursor基本用法的详细介绍

时间:2024-01-12 22:05:06 

由于这个游标 执行一下就相当于SELECT一下 其效率不敢恭维也没做深入研究。


 table1结构如下
 id    int
 name  varchar(50)

 declare @id int
 declare @name varchar(50)
 declare cursor1 cursor for         --定义游标cursor1
 select * from table1               --使用游标的对象(跟据需要填入select文)
 open cursor1                       --打开游标

 fetch next from cursor1 into @id,@name  --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中

 while @@fetch_status=0           --判断是否成功获取数据
 begin
 update table1 set name=name+'1'
 where id=@id                           --进行相应处理(跟据需要填入SQL文)

 fetch next from cursor1 into @id,@name  --将游标向下移1行
 end

 close cursor1                   --关闭游标
 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
0
投稿

猜你喜欢

  • python连接远程ftp服务器并列出目录下文件的方法

    2023-10-20 10:35:04
  • MySQL索引概念及七种索引类型分享介绍

    2024-01-23 03:55:27
  • 用 Python 定义 Schema 并生成 Parquet 文件详情

    2021-09-24 14:14:18
  • Python自然语言处理之词干,词形与最大匹配算法代码详解

    2023-07-23 04:48:37
  • “您无权查看该网页”的原因和解决方法

    2008-03-24 16:57:00
  • 深入理解Go语言中的Dispatcher

    2024-02-03 03:27:18
  • Golang指针隐式间接引用详解

    2024-02-22 02:29:53
  • 小议sqlserver数据库主键选取策略

    2024-01-25 15:03:52
  • Python基础第三方模块requests openpyxl

    2023-09-24 04:31:32
  • python Django 创建应用过程图示详解

    2022-10-29 03:04:40
  • python分分钟绘制精美地图海报

    2021-11-10 11:44:25
  • 分析解决Python中sqlalchemy数据库连接池QueuePool异常

    2024-01-16 12:18:19
  • 支持多风格变换的ASP分页类

    2007-10-13 18:48:00
  • Python的Flask框架使用Redis做数据缓存的配置方法

    2024-01-21 18:37:47
  • Phar反序列化超详细介绍

    2023-06-05 07:06:02
  • 利用PyQt5+Matplotlib 绘制静态/动态图的实现代码

    2021-11-05 06:22:11
  • Python 程序报错崩溃后如何倒回到崩溃的位置(推荐)

    2021-01-08 16:14:34
  • Python打造虎年祝福神器的示例代码

    2021-01-08 17:23:43
  • Python Django框架设计模式详解

    2021-01-16 12:52:40
  • python 图像判断,清晰度(明暗),彩色与黑白实例

    2023-09-04 12:58:00
  • asp之家 网络编程 m.aspxhome.com