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

            1.声明游标
                DECLARE 游标名 CURSOR SELECT语句(注:此处一定是SELECT语句)
            2.打开游标
               OPEN 游标名
            3.读取游标数据
               Fetch [Next | Prior | First | Last | Absolute n | Relative n ]  From 游标名 INTO @name1,@name2...
                WHILE(@@FETCH_STATUS = 0)
                             BEGIN
                             --要执行的SQL语句
                             FETCH NEXT FROM 游标名
                             END

            4.关闭游标
                    CLOSE 游标名。关闭后不能对游标进行读取等操作,但可以使用OPEN语句再次打开
            5.释放游标
                    DEALLOCATE 游标名。即删除游标,不可再使用

    例子:

    declare @index int;
    declare @userId uniqueidentifier;
    set @index=1;
    declare user_cur cursor for select UserId from T_User order by CreateTime desc
    open user_cur
    fetch next from user_cur into @userId
    while (@@FETCH_STATUS=0)
    begin
      update T_User set Sort=@index where UserId=@userId;
      set @index=@index+1;
      fetch next from user_cur into @userId
    end
    
    close user_cur;
    deallocate user_cur;

     参考链接:http://www.cnblogs.com/youngberry/archive/2009/07/17/1525647.html

  • 相关阅读:
    gulp通过http-proxy-middleware开启反向代理,实现跨域
    一些我常用的css 或者 js
    四舍五入
    生成 SSH 公钥
    对象转为数组 用lodash
    廖雪峰的官方网站
    window对象
    字符串
    简单得日期
    LeetCode 113. Path Sum II 20170705 部分之前做了没写的题目
  • 原文地址:https://www.cnblogs.com/Gylianger/p/6935499.html
Copyright © 2011-2022 走看看