oracle 10g 游标

来源:互联网 发布:2017明教成男捏脸数据 编辑:程序博客网 时间:2024/05/14 00:14
  1. 游标:
  2.  游标是映射在结果集中行数据上的位置实体。Oracle系统在内存中开辟一个工作区,从中存放select语句返回的查询结果。
  3. 根据SQL语句的操作类型,游标分为隐式游标和显式游标。
  4. 1 显示游标
  5. (1)定义游标
  6.    定义游标名以及作为游标体的select查询语句,即使用一个游标与select语句建立起联系。
  7. create <cursor_name> [(parameter[,parameter]...)] is select_statement;
  8. 游标参数只能是输入参数,其格式为:
  9. parameter_name[in] datetype[{:=|default}expression]
  10. 在指定数据类型时,不能在数据类型中添加长度约束,如number(6),vhar(12)等都是错误的。在定义游标时不能有into子句,
  11. 如果要指定游标查询结果的顺序,可以使用order by子句。
  12. (2)打开游标
  13. open cursor_name[([parameter=>]value[,[parameter=>]value]...)];
  14. %isopen判断游标是否打开。
  15. (3)提取游标数据
  16. fetch <cursor_name> into {variable_lis|record_variable};
  17. declare
  18. cursor c_stu is select * from stuInfo order by stuAge; --定义游标
  19. counts stuInfo.stuAge% type; --定义stuInfo.stuAge类型的变量 数据类型与stuAge相同
  20. rec_stu stuInfo% rowtype; --定义记录集变量
  21. begin
  22.  open c_stu;
  23.  fetch c_stu into rec_stu;
  24.  dbms_output.put_line('姓名: '||rec_stu.stuName);
  25.  close c_stu;
  26.  for rec_stu in c_stu loop
  27.   dbms_output.put_line('姓名'||rec_stu.stuName||' 学号'||rec_stu.stuNo);
  28.  end loop;
  29. end;
  30. (4)关闭游标
  31. close <cursor_name>;
  32. (5)游标属性
  33. %isopen     布尔型,如果游标打开,则为true
  34. %notfound   布尔型,如果没有返回行,则为true  
  35. %found      布尔型,如果有返回行,则为true
  36. %rowcount   数值型,当前为止从工作返回的总行数
  37. create or replace procedure cur_property is
  38. begin
  39.    update stuInfo set stuAddress='北京,我爱你!' where stuName='Oracle';
  40.    if SQL% notfound then
  41.      dbms_output.put_line('更新失败!');
  42.    else
  43.        dbms_output.put_line('更新成功!');
  44.    end if;
  45. end cur_property;
  46. (6)游标属性的引用方法
  47. 显示游标: 游标名.<属性名>
  48. 隐式游标:SQL.<属性名>
  49. (7)游标与for循环
  50. 语法:
  51. for <record_name> in(<corsor_name>[{parameter[,parameter]...}] | (query_difintition))
  52. loop
  53.  <statements>
  54. end loop;
  55. (8)游标的where current of子句
  56. 语法:
  57. select...from...for updae[of column[,column]...][nowait]
  58. 若在游标中使用了for update子句,则在delete和update语句中可以使用where current of<cursor_name>
  59. 子句,以修改或删除游标结果集当前行所对应的表中的数据行。
  60. --声明此游标可以更新
  61. declare cursor cur_stu is select * from stuMark for update of labExam;
  62. begin
  63.  for rec_stu in cur_stu loop
  64.    update stuMark set labExam=labExam-10 where current of cur_stu;
  65.  end loop;
  66.  commit;
  67. end;
  68. /
  69. (9)隐式游标:
  70.  隐式游标的名字一律为SQL,这是由Oracle系统定义的,对于隐式游标的所有操作都是由Oracle系统自动完成的。
  71. --stuInfo和stuMark表具有参照关系,所以,在删除stuInfoo表中的所有员工时,若该部门中没有学生,则在stuMark
  72. 表中删除。
  73. declare v_stuNo stuInfo.stuNo% type:='s25301';
  74. begin
  75.  delete from stuInfo where stuNo=v_stuNo;
  76.  if SQL% notfound then
  77.    delete from stuMark where stuNo='s25301';
  78.  end if;
  79. end;
  80. (10)游标变量:
  81. 定义游标变量语法:
  82. type <ref_cursor_type_name> is ref cursor[return <return_type>];
  83. 声明游标变量语法:
  84. <cursor_variable> <ref_cursor_type_name>;
  85. --定义游标变量
  86. declare
  87. type t_stuInfo is ref cusor return stuInfo% rowtype;
  88. type t_stuInforecord is record(v_stuName stuInfo.stuName% type,
  89.              v_stuNo stuInfo.stuNo% type,
  90.                          v_stuSex stuInfo.stuSex% type,
  91.              v_stuAge stuInfo.stuAge% type,
  92.              v_stuSeat stuInfo.stuSeat% type,
  93.              v_stuAddress stuInfo.stuAddress% type);
  94. --声明游标变量
  95. cur_stuInfo t_stuInfo;
  96. type t_cur_stuInfo is ref cursor return t_stuInforecord;
  97. (11)为查询打开游标变量
  98. open <cursor_variable> for select_statement
  99. 使用游标的例子:
  100. 1定义包
  101. create or replace package types is
  102.   type cursor_type is ref cursor;
  103. end types;
  104. 2定义使用游标的存储过程
  105. create or replace procedure getStuInfo(v_stuNo in stuInfo.stuNo% type,cur_recordset out types.cursor_type) is
  106. begin
  107.   open cur_recordset for
  108.        select * from stuInfo where stuNo=v_stuNo order by stuName;
  109. end getStuInfo;
  110. 3 在PL/SQL中引用已定义好的游标
  111. set serveroutput on 1000
  112. declare
  113.  v_cursor types.cursor_type; 
  114.  v_stuName stuInfo.stuName% type;
  115.  v_stuNo stuInfo.stuNo% type;
  116.  v_stuSex stuInfo.stuSex% type;
  117.  v_stuAge stuInfo.stuAge% type;
  118.  v_stuSeat stuInfo.stuSeat% type;
  119.  v_stuAddress stuInfo.stuAddress% type;
  120.  begin
  121.   getStuInfo(v_stuNo=>'s25301',cur_recordset =>v_cursor );
  122.   loop
  123.    --从v_cursor取出数据
  124.    fecth v_cursor into v_stuName,v_stuNo,v_stuSex,v_stuAge,v_stuSeat,v_stuAddress;
  125.    exit when v_cursor% notfound;
  126.    dmbs_output.put_line(v_stuName||'|'v_stuNo||'|'v_stuSex ||'|'v_stuAge||'|' v_stuSeat ||'|'v_stuAddress );
  127.   end loop;
  128.   close v_cursor;
  129.  end;
  130.  /