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

     1 /****************************
     2            说明:游标使用
     3            时间:07/07/15
     4 ******************************/
     5 use student_15
     6 go
     7 declare student_cur cursor
     8 
     9    for select * from student
    10 go
    11 
    12 declare @sid int
    13 declare @sname varchar(20)
    14 declare @ssex char(1)
    15 declare @sage int
    16 declare @classid int
    17 
    18 open student_cur
    19    fetch next from student_cur into @sid,@sname,@ssex,@sage,@classid
    20     while @@fetch_status=0
    21     begin
    22       print @sname
    23       fetch next from student_cur into @sid,@sname,@ssex,@sage,@classid
    24     end
    25 close student_cur
    26 deallocate student_cur
    27 /*******************************
    28 
    29       说明:游标的操作(update)
    30       时间:07/07/15
    31 *******************************/
    32 use student_15
    33 go
    34 select * from student
    35 go
    36 
    37 declare student_cur cursor
    38     scroll   --游标的移动位置
    39     for select * from student
    40     for update of sname,sage
    41 go   
    42 declare @sid int
    43 declare @sname varchar(20)
    44 declare @ssex char(1)
    45 declare @sage int
    46 declare @classid int
    47 
    48 open student_cur
    49    fetch first from student_cur into @sid,@sname,@ssex,@sage,@classid 
    50      /*更新游标数据*/
    51    update student set sage=19,sname='11' where current of student_cur 
    52     while @@fetch_status =0    begin
    53 
    54           fetch next from student_cur into @sid,@sname,@ssex,@sage,@classid
    55          
    56           if @sid=1003   begin
    57            update student set sage=19,sname='11' where current of student_cur 
    58           end
    59           
    60      end
    61 close student_cur
    62 deallocate student_cur
    63 
    64 /*************游标操作删除***************/
    65 use student_15
    66 go
    67 
    68 declare student_cur cursor
    69     scroll
    70     for select * from student
    71     for update of sid
    72 go
    73 
    74 declare @sid int
    75 declare @sname varchar(20)
    76 declare @ssex char(1)
    77 declare @sage int
    78 declare @classid int
    79 
    80 open student_cur
    81 
    82       fetch first from student_cur into @sid,@sname,@ssex,@sage,@classid
    83 
    84       while @@fetch_status=0
    85 
    86          begin
    87 
    88       fetch next from student_cur into @sid,@sname,@ssex,@sage,@classid
    89 
    90       if @sid=1003
    91     
    92         begin
    93   
    94          delete student where current of student_cur
    95    
    96         end
    97      end
    98 close student_cur
    99 deallocate student_cur 
  • 相关阅读:
    PYthon继承链(egg)的思考和实战
    C++不同类型变量参与运算时的规则
    qt通过QFileDialog获取文件路径&保存文件&选择文件夹
    visual studio 2015调试程序
    C++Primer第五版——书店程序实现
    git rm命令 & git reset和checkout区别
    git diff命令输出解释 & git checkout还原文件到特定版本
    Qt使用connect传参数的两种方式
    QFrame的setFrameStyle函数 && QPalette设置背景
    tr函数作用
  • 原文地址:https://www.cnblogs.com/dqh123/p/9470243.html
Copyright © 2011-2022 走看看