存储过程游标使用
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
存储过程游标使用
选用何种游标?
显示游标分为:普通游标,参数化游标和游标变量三种。
下面以一个过程来进行说明
Java代码
1.create or replace procedure proccursor(p varchar2)
2.as
3.v_rownum number(10) := 1;
4.cursor c_postype is select pos_type from pos_type_tbl wh ere rownum =1;
5.cursor c_postype1 is select pos_type from pos_type_tbl w here rownum = v_rownum;
6.cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
7.type t_postype is ref cursor ;
8.c_postype3 t_postype;
9.v_postype varchar2(20);
10.begin
11.open c_postype;
12.fetch c_postype into v_postype;
13.dbms_output.put_line(v_postype);
14.close c_postype;
15.open c_postype1;
16.fetch c_postype1 into v_postype;
17.dbms_output.put_line(v_postype);
18.close c_postype1;
19.open c_postype2(1);
20.fetch c_postype2 into v_postype;
21.dbms_output.put_line(v_postype);
22.close c_postype2;
23.open c_postype3 for select pos_type from pos_type_t
bl where rownum =1;
24.fetch c_postype3 into v_postype;
25.dbms_output.put_line(v_postype);
26.close c_postype3;
27.end;
cursor c_postype is select pos_type from pos_type_tbl where rownum =1
这一句是定义了一个最普通的游标,把整个查询已经写死,调用时不可以作任何改变。
cursor c_postype1 is select pos_type from pos_type_tbl where rownum = v_rownum;
这一句并没有写死,查询参数由变量v_rownum来决定。
需要注意的是v_rownum必须在这个游标定义之前声明。
cursor c_postype2(p_rownum number) is select pos_type from pos_type_tbl where rownum = p_rownum;
这一条语句与第二条作用相似,都是可以为游标实现动态的查询。
但是它进一步的缩小了参数的作用域范围。
但是可读性降低了不少。
type t_postype is ref cursor ;
c_postype3 t_postype;
先定义了一个引用游标类型,然后再声明了一个游标变量。
open c_postype3 for select pos_type from pos_type_tbl where rownum =1;
然后再用open for 来打开一个查询。
需要注意的是它可以多次使用,用来打开不同的查询。
从动态性来说,游标变量是最好用的,但是阅读性也是最差的。
注意,游标的定义只能用使关键字IS,它与AS不通用。