ITPub博客

首页 > IT职业 > IT生活 > 游标

游标

IT生活 作者:xtky_limi 时间:2021-09-09 09:44:48 0 删除 编辑

--显式游标

图片描述

declare  cursor c_c is select * from scott.emp;--定义游标  r_r scott.emp%rowtype;                --定义变量[rowtype为行类型]begin  open c_c;                        --打开游标  loop                             --简单循环    fetch c_c into r_r;            --将数据放入变量[执行FETCH语句时,每次返回一个数据行,然后自动将游标移动指向下一个数据行。]    dbms_output.put_line(r_r.job); --输出行变量中字段数据    exit when c_c%notfound;        --如果没有下一条数据[退出条件 found有下条数据 notfound没有下条数据]  end loop;  close c_c;                       --关闭游标end;

图片描述

--带有参数的游标

图片描述

declare  cursor c_c(mid number default 7521) is select * from scott.emp where empno=mid;--定义游标  r_r scott.emp%rowtype;                --定义变量[rowtype为行类型]begin  open c_c(7788);                       --打开游标  loop                                  --简单循环    fetch c_c into r_r;     dbms_output.put_line(r_r.job||' '||r_r.empno); --输出行变量中字段数据    exit when c_c%notfound;             --如果没有下一条数据[退出条件 found有下条数据 notfound没有下条数据]  end loop;  close c_c;                            --关闭游标end;

图片描述

 --隐式游标

图片描述

declare  cursor c_c is    select * from scott.emp;  r_r scott.emp %rowtype;begin  for v_r in c_c loop                --自动打开游标,自动检测NOTFOUND    dbms_output.put_line(v_r.job);  end loop;  --隐含关闭游标end;

图片描述

--游标变量

图片描述

declare  type m_c_type is ref cursor; --定义游标类型 m_c_type  c_c m_c_type;                --声明游标变量  r_r scott.emp%rowtype;begin  open c_c for                 --打开游标变量    select * from scott.emp;  loop    fetch c_c      into r_r;    dbms_output.put_line(r_r.job);    exit when c_c%notfound;  end loop;end;

图片描述

--动态定义游标 

图片描述

declare  msql    varchar2(111) := 'select * from emp';  tbl_emp emp%rowtype;  type cur_type is ref CURSOR;  cur cur_type;begin  OPEN cur for msql; --打开动态定义的游标  LOOP    FETCH cur      into tbl_emp; --循环赋值    EXIT WHEN cur%NOTFOUND; --跳出条件    dbms_output.put_line(tbl_emp.empno || '  ' || tbl_emp.ename); --打印  END LOOP; end;

图片描述

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4383/viewspace-2806786/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    88
  • 访问量
    676