oracle 游标相关资料
游标 概述:游标是系统为用户开设的一个数据缓冲区,存放 SQL 语句的执行结果。 我们可以把游标理解为 PL/SQL 中的结果集,把游标当中一个集合 1:在声明区声明游标 cursor 游标名称 is/as sql语句; 注意用is/as连接 2:基本语法 open 游标名称; --打开游标,须加分号 loop fetch 游标名称 into xx; 从游标中抓取放在xx中 exit when 游标名称%notfound; 不能无限循环,抓取完了,必须退出 end loop; close 游标名称 案例一:一般的游标的操作 --游标的操作 select * from t_pricetable where ownertypeid=1 --在声明区声明游标并带返回值 declare --必须创建declare声明区,声明游标必须带介词is/as v_pricetable t_pricetable%rowtype; --带返回值参数,用来存储提取的游标 cursor cur_pricetable is select * from t_pricetable where ownertypeid=1; begin --开启游标 open cur_pricetable; loop fetch cur_pricetable into v_pricetable; -- 游标抓取完后,自动退出 exit when cur_pricetable%notfound; --执行打印语句 dbms_output.put_line( ‘价格:‘||v_pricetable.price ||‘吨位:‘||v_pricetable.minnum||‘-‘||v_pricetable.maxnum ); end loop; close cur_pricetable; --关闭游标 end; 案例二:带参游标 --带参数游标的操作 select * from t_pricetable where ownertypeid=1 --在声明区声明游标并带返回值 declare v_pricetable t_pricetable%rowtype; --带返回值参数,用来存储提取的游标 --声明带参的游标(v_ownertype(游标的参数) number) 将ownertypeid(业主类型)直接使用游标的参数v_ownertype 就相当于索引被游标替换掉了 cursor cur_pricetable is select * from t_pricetable where ownertypeid=1; begin --开启游标 open cur_pricetable; --当带参的话,()中可以写业主类型 loop fetch cur_pricetable into v_pricetable; -- 游标抓取完后,自动退出 exit when cur_pricetable%notfound; --执行打印语句 dbms_output.put_line( ‘价格:‘||v_pricetable.price ||‘吨位:‘||v_pricetable.minnum||‘-‘||v_pricetable.maxnum ); end loop; close cur_pricetable; --关闭游标 end; 案例三:循环提取游标值 declare cursor cur_pricetable(v_ownertypeid number) is select * from T_PRICETABLE where ownertypeid=v_ownertypeid;--定义游标 begin for v_pricetable in cur_pricetable(3) loop dbms_output.put_line(‘价格:‘||v_pricetable.price ||‘吨位:‘||v_pricetable.minnum||‘-‘||v_pricetable.maxnum ); end loop; end ;
? 参游标是指带有参数的游标。在定义了参数游标之后,当使用不同参数值多次打开游标时,可以生成不同的结果集。定义参数游标的语法如下: CURSOR cursor_name(parameter_name datetype) IS select_statement; 注意,当定义参数游标时,游标参数只能指定数据类型,而不能指定长度。当定义参数游标时,一定要在游标子查询的where子句中引用该参数,否则就失去了定义参数游标的意义。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |