zoukankan      html  css  js  c++  java
  • 游标的使用

    -- 游标详解

    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

  • 相关阅读:
    C#使用Xamarin开发可移植移动应用(5.进阶篇显示弹出窗口与通讯中心)附源码
    C#使用Xamarin开发可移植移动应用(4.进阶篇MVVM双向绑定和命令绑定)附源码
    C#使用Xamarin开发可移植移动应用(3.Xamarin.Views控件)附源码
    C#使用Xamarin开发可移植移动应用(2.Xamarin.Forms布局,本篇很长,注意)附源码
    C#使用Xamarin开发可移植移动应用(1.入门与Xamarin.Forms页面),附源码
    ASP.NET Core之跨平台的实时性能监控(2.健康检查)
    Android Studio 快捷键:重载与重写、try catch代码块、导包 快捷键
    新版本jQuery对动态添加元素绑定点击事件实例
    ssm框架中,mybatis的sql语句日志输出
    maven环境下的ssm框架上传excel 案例
  • 原文地址:https://www.cnblogs.com/yu-and-ze/p/4717338.html
Copyright © 2011-2022 走看看