Use of SQL Midstream (RPM)

Source: Internet
Author: User
Tags rowcount

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 8 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 一般情况下,我们用SELECT这些查询语句时,都是针对的一行记录而言, 如果要在查询分析器中对多行记录(即记录集)进行读取操作时,则需要使用到游标或WHILE等循环游标的类型:   1、静态游标(不检测数据行的变化)   2、动态游标(反映所有数据行的改变)   3、仅向前游标(不支持滚动)   4、键集游标(能反映修改,但不能准确反映插入、删除)游标使用顺序:    1、定义游标    2、打开游标    3、使用游标    4、关闭游标    5、释放游标Transact-SQL: declare 游标名 cursor [LOCAL | GLOBAL][FORWARD_ONLY | SCROLL][STATIC | KEYSET | DYNAMIC ] [READ_ONLY | SCROLL_LOCKS]    for selet语句   [forupdate[of 列名[,列名]]  注:LOCAL 局部游标     GLOBAL 全局游标      FORWARD_ONLY 仅向前  SCROLL 滚动      STATIC 静态  KEYSET 键集 DYNAMIC 动态      READ_ONLY 只读 SCROLL_LOCKS 锁定游标当前行获取游标的数据   FETCH [[NEXT | PRIOR | FIRST | LAST |   ABSOLUTE{ n | @nvar | RELATIVE { n | @nvar}]   From ] 游标名 [into 变量]   注:     NEXT下一行  PRIOR上一行  FIRST 第一行     LAST最后一行  ABSOLUTE n 第n行     RELATIVE n 当前位置开始的第n行     into 变量 把当前行的各字段值赋值给变量游标状态变量:     @@fetch_status  游标状态          0 成功  -1 失败  -2 丢失     @@cursor_rows 游标中结果集中的行数         n 行数 -1 游标是动态的  0 空集游标 操作游标的当前行:    current of 游标名例1:利用游标对查询的数据集进行遍历 use pubsgo declare @auid char(12),@aulname varchar(20),@aufname varchar(20), @st char(2),@auinfo varchar(50)declare auth_cur cursor forselect au_id, au_lname, au_fname, statefrom authorsopen auth_curfetch next from auth_cur into @auid,@aulname,@aufname, @stwhile (@@fetch_status=0)  begin    print ‘作者编号: ‘[email protected]    print ‘作者姓名: ‘[email protected]+‘,‘[email protected]    print ‘所在州: ‘[email protected]    print ‘--------------------------‘    fetch next from auth_cur into @auid,@aulname,@aufname, @st  endclose auth_curdeallocate auth_cur例2:利用游标修改、删除数据 declare auth_cur cursor scroll forselect au_id, au_lname, au_fname, statefrom authors for update of au_lnameopen auth_curdeclare @rowcount intset @rowcount = 6fetch absolute @rowcount from auth_cur  --将变量@rowcount标识的行设置为当前行--下面1行是利用游标进行修改操作update authors set au_lname=‘张‘ where current of auth_cur --修改游标中的当前行--下面1行是利用游标进行删除操作delete from authors where current of auth_cur

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.