[独孤九剑]Oracle知识点梳理(七)数据库常用对象之Cursor

本系列链接导航:

[独孤九剑]Oracle知识点梳理(一)表空间、用户

[独孤九剑]Oracle知识点梳理(二)数据库的连接

[独孤九剑]Oracle知识点梳理(三)导入、导出

[独孤九剑]Oracle知识点梳理(四)SQL语句之DML和DDL

[独孤九剑]Oracle知识点梳理(五)数据库常用对象之Table、View

[独孤九剑]Oracle知识点梳理(六)数据库常用对象之Procedure、function、Sequence

[独孤九剑]Oracle知识点梳理(七)数据库常用对象之Cursor

[独孤九剑]Oracle知识点梳理(八)常见Exception

[独孤九剑]Oracle知识点梳理(九)数据库常用对象之package

[独孤九剑]Oracle知识点梳理(十)%type与%rowtype及常用函数

5.6、cursor操作

  游标的使用场景大致有:

    a) 显示游标:function或procedure中,用于获取某些值进行遍历操作

    b) 动态游标:procedure中,用于返回查询结果

5.6.1、显示游标:在代码段、function、procdeure中创建cursor,用于取值

  • 隐式游标
 declare
cursor myCursor is
select name from person;
  vNames varchar2(128);
begin
  --遍历游标
  for c in myCursor loop --隐式打开、关闭游标
    vNames:=vNames||','||c; -- 可以包含复杂逻辑
  end loop;
end;
  • 显示游标
 declare
  cursor myCursor is --定义游标
    select * from person;
  my_c myCursor%rowtype --定义游标变量
begin
  open myCursor;
  loop
    fetch myCursor into my_c;
    exit when c%notfound;
    /*code here*/ --编写复杂逻辑
  end loop;   Exception
    when others then
      close myCursor;   if myCursor%isopen then
    close myCursor;
end;

  上面写的两个列子比较简单,下面是网上摘抄显示游标例子,很详细:

  对于显式游标的运用分为四个步骤:

  • 定义游标---Cursor [Cursor Name] IS;
  • 打开游标---Open [Cursor Name];
  • 操作数据---Fetch [Cursor name]
  • 关闭游标---Close [Cursor Name],这个Step绝对不可以遗漏。

  以下是三种常见显式Cursor用法。
1)Set serveroutput on;

 declare
  ---define Cursor
  Cursor cur_policy is
    select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account
        from t_contract_master cm
        where cm.liability_state = 2
            and cm.policy_type = 1
            and cm.policy_cate in ('','','')
            and rownum < 5
        order by cm.policy_code desc;
  curPolicyInfo cur_policy%rowtype;---定义游标变量
Begin
  open cur_policy; ---open cursor
  Loop
    --deal with extraction data from DB
    Fetch cur_policy into curPolicyInfo;
    Exit when cur_policy%notfound;     Dbms_Output.put_line(curPolicyInfo.policy_code);
  end loop;
  Exception
    when others then
      close cur_policy;
    Dbms_Output.put_line(Sqlerrm);   if cur_policy%isopen then
    --close cursor
    close cur_policy;
  end if;
end; /

2) Set serveroutput on; 

 declare
  Cursor cur_policy is
    select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account
      from t_contract_master cm
      where cm.liability_state = 2
        and cm.policy_type = 1
        and cm.policy_cate in ('','','')
        and rownum < 5
      order by cm.policy_code desc;
  v_policyCode t_contract_master.policy_code%type;
  v_applicantId t_contract_master.applicant_id%type;
  v_periodPrem t_contract_master.period_prem%type;
  v_bankCode t_contract_master.bank_code%type;
  v_bankAccount t_contract_master.bank_account%type;
Begin
  open cur_policy;
  Loop
    Fetch cur_policy into v_policyCode,
                v_applicantId,
                v_periodPrem,
                v_bankCode,
                v_bankAccount;
    Exit when cur_policy%notfound;     Dbms_Output.put_line(v_policyCode);
  end loop;
  Exception
28    when others then
      close cur_policy;
    Dbms_Output.put_line(Sqlerrm);   if cur_policy%isopen then
    close cur_policy;
  end if;
end;
/

3)Set serveroutput on; 

 declare
  Cursor cur_policy is
    select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account
        from t_contract_master cm
        where cm.liability_state = 2
          and cm.policy_type = 1
          and cm.policy_cate in ('','','')
          and rownum < 5
        order by cm.policy_code desc;
Begin
  For rec_Policy in cur_policy loop
    Dbms_Output.put_line(rec_policy.policy_code);
  end loop;
  Exception
    when others then
    Dbms_Output.put_line(Sqlerrm); end; /

5.6.2、动态游标:在procdeure中使用cursor,用于返回查询结果

  与隐式Cursor,显式Cursor的区别:

  • Ref Cursor是可以通过在运行期间传递参数来获取数据结果集。
  • 而另外两种Cursor(隐式游标和显示游标),是静态的,在编译期间就决定数据结果集。

5.6.2.1、定义动态游标

 Declare
  ---define cursor type name
  type cur_type is ref cursor;
  cur_policy cur_type;
  sqlStr varchar2(500);
  rec_policy t_contract_master%rowtype;
begin
  ---define 动态Sql
  sqlStr := 'select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,
              cm.bank_account from t_contract_master cm
          where cm.liability_state = 2
              and cm.policy_type = 1
              and cm.policy_cate in (2,3,4)
              and rownum < 5
            order by cm.policy_code desc ';
  ---Open Cursor
  open cur_policy for sqlStr;
  loop
    fetch cur_policy into rec_policy.policy_code, rec_policy.applicant_id, rec_policy.period_prem,
                  rec_policy.bank_code,rec_policy.bank_account;
    exit when cur_policy%notfound;     Dbms_Output.put_line('Policy_code:'||rec_policy.policy_code);   end loop;
close cur_policy; end;
/

另外,在定义package时,可以在包头中定义动态游标类型,在包体中使用,如在procedure中用于定义返回参数类型。详见package的操作。

上一篇:[独孤九剑]Oracle知识点梳理(十)%type与%rowtype及常用函数


下一篇:sqlite 时间函数及时间处理