1 游标的作用: 2 1.定位到结果集中的某一行。 3 2.对当前位置的数据进行读写。 4 3.可以对结果集中的数据单独操作,而不是整行执行相同的操作。 5 4.是面向集合的数据库管理系统和面向行的程序设计之间的桥梁。 6
7 游标的种类: 8 1.静态游标:操作结果在重新打开才有效,消耗少 9 2.动态游标:操作结果在下一次操作可用,消耗多 10 3.只进游标:从头到尾提取数据,操作结果提取时可见,提取后不可见 11 4.键集Y驱动游标:操作结果被标识的数据可见,未被标识的数据不可见,消耗适中 14
15 定义一个游标: 16 declare cursor_name cursor [cursor_type] (cursor_type有static,dynamic,forward_only,keyset) 17 for 查询语句结果集 18
19 打开一个游标:
20 open cursor_name 21
22 使用游标提取数据方式: 23 fetch 某参数 from cursor_name into 变量列表 24 某参数说明: 25 1.first:定位到结果集第一行 26 2.last:定位到结果集的最后一行 27 3.prior:定位到上一行 28 4.next:定位到下一行 29 5.absoute n:定位到从第一行开始第n行 30 6.relative n:定位到从当前位置开始第n行 31
32 游标相关状态全局变量使用: 33 建立一个循环读取可能: 34 declare 参数1 参数类型,参数2 参数类型... 35 fetch first from cursor_name into 参数1 参数类型,参数2 参数类型... 36 while @@fetch_status = 0
37 begin 38 print '参数信息'
39 fetch next from cursor_name into 参数1 参数类型,参数2 参数类型... 40 end 41
42 更新、删除游标数据: 43 update table_name set colume_name = value[,...] where current of cursor_name 44 delete table_name where current of cursor_name 45
46 关闭、删除游标: 47 close cursor_name 48 deallocate cursor_name