博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
游标的使用
阅读量:4601 次
发布时间:2019-06-09

本文共 2402 字,大约阅读时间需要 8 分钟。

-- 游标详解

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  --将游标向下移行,获取的数据放入之前定义的变量@id,@name中

 

 while @@fetch_status=0           --判断是否成功获取数据

 begin

 update table1 set name=name+'1'

 where id=@id                           --进行相应处理(跟据需要填入SQL文)

 

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

 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 游标名称 (删除游标)

 

--测试数据

create table tmp1  (

   ID                   int                      not null,

   val       varchar(10),

   constraint PK_tmp1 primary key (ID)

);

 

create table tmp2  (

   ID                   int                      not null,

   vals       varchar(100),

   constraint PK_tmp2 primary key (ID)

);

 

insert into tmp1(id, val) values  (1, 'test');

insert into tmp1(id, val) values  (2, 'test2');

insert into tmp1(id, val) values  (3, 'test3');

insert into tmp1(id, val) values  (4, 'test4');

insert into tmp1(id, val) values  (5, 'test5');

 

insert into tmp2(id, vals) values  (1, '1,2');

insert into tmp2(id, vals) values  (2, '1,3');

insert into tmp2(id, vals) values  (3, '2,5');

insert into tmp2(id, vals) values  (4, '1,2,3,4,5');

 

 

--存储过程

drop procedure proc_tmp_1

go

 

 

CREATE PROCEDURE proc_tmp_1 AS

begin

    declare @vals varchar(500)

    declare @id int

    declare @vals2 varchar(1000)

    declare @command varchar(1000)

    declare @vals3 varchar(1000)

    declare @cmd varchar(1000) 

    declare cursor_tmp_1 cursor for SELECT id, vals FROM tmp2

    open cursor_tmp_1

    fetch next from cursor_tmp_1 into @id, @vals

    while @@fetch_status = 0

    begin 

       set @vals3 = ''

       set @cmd = 'declare cursor_tmp_2 cursor for select val from tmp1 where id in (' + @vals + ')'

       EXEC (@cmd)

       open cursor_tmp_2

       fetch next from cursor_tmp_2 into @vals2

       while @@fetch_status = 0

       begin

           if (@vals3 <> '')

           begin

              set @vals3 = @vals3 + ','

           end

           SET @vals3 = @vals3 + @vals2          

           fetch next from cursor_tmp_2 into @vals2

       end

       set @command = 'update tmp2 set vals =''' + @vals3 + ''' where id =' + convert(char, @id)

       print @command

       EXEC (@command)

       fetch next from cursor_tmp_1 into @id, @vals    

       close cursor_tmp_2

       deallocate cursor_tmp_2

    end

    close cursor_tmp_1  

    deallocate cursor_tmp_1

end

go

 

--执行存储过程

EXECUTE  proc_tmp_1

Go

转载于:https://www.cnblogs.com/yu-and-ze/p/4717338.html

你可能感兴趣的文章
React之特点及常见用法
查看>>
【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
查看>>
优云软件助阵GOPS·2017全球运维大会北京站
查看>>
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>
MySQL数据库免安装版配置
查看>>
你必知必会的SQL面试题
查看>>
html5 Canvas绘制时钟以及绘制运动的圆
查看>>
Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween
查看>>
JavaScript空判断
查看>>
洛谷 P1439 【模板】最长公共子序列(DP,LIS?)
查看>>
python timeit
查看>>
Wireless Network 并查集
查看>>