• Sql Server 之游标


      一般来说,我们通过SQL一般是面向集合进行数据操作,但是游标提供给我们更加强大的功能可以对数据集合进行逐行遍历有选择性的操作处理。当然游标也有其不可避免的缺陷就是:低效和复杂。所以一般正常的操作处理不会选择使用游标进行数据操作。

      游标的使用:

    declare testCur cursor                   

      for select ID,gradefrom student where grade <60 or grade is null   ---声明游标

    open testCur                                                                      ---打开游标

    declare @ID       decimal(10,0)
    declare @grade  varchar(100)                            ---定义游标中的参数

      fetch next from testCur into @ID,@grade                ---当游标被打开时,行指针将指向该游标集第1行之前,如果要读取游标集中的第1行数据,必须移动行指针使其指向第1行

    while @@fetch_status=0                                                    --- @@fetch_status=0 说明游标活动(fetch next from testCur)成功,-1 表示FETCH 语句失败或此行不在结果集中,-2 被提取的行不存在

      if @grade is not null                         ---加入逻辑判断

      begin

        update student set grade=60 where grade<60 and id=@ID     --根据游标

        fetch next from testCur into @ID,@grade

      end

      else

      begin

        update student set grade='缺考' where id=@ID

        fetch next from testCur into @ID,@grade

      end

    close testCur                              ---关闭游标

    deallocate testCur                            ---删除游标

    Best Wish !
  • 相关阅读:
    通过 VB5 创建 ActiveX DLL 文件并用 ASP 调用一例
    Autocad VBA初级教程
    自学资料第一集
    Linux虚拟化:10个不得不爱的理由
    EXCEL VBA编程的一些小结
    FAQ 工作薄及工作表
    很重要的EXCEL使用技巧
    Excel VBA编程的常用代码
    VBA生成一个CorelDraw工具栏
    支付宝,网银在线,快钱 3大支付接口的集成与对比,统合实现
  • 原文地址:https://www.cnblogs.com/ultimateWorld/p/4761293.html
Copyright © 2020-2023  润新知