标签:SYS_REFCURSOR REF CURSOR 动态游标 ORACLE 动态游标
REF 游标:示例1:强类型REF游标
DECLARE
TYPE REF_CURSOR IS REF CURSOR RETURN DJ_DJB%ROWTYPE;
------游标仅能打开DJ_DJB表的数据
REF_C REF_CURSOR;
----游标变量
V_DJB DJ_DJB%ROWTYPE;
SELECTION VARCHAR2(1):=(‘&请输入:‘);
BEGIN
IF SELECTION=‘1‘ THEN
OPEN REF_C FOR SELECT * FROM DJ_DJB WHERE SLBH LIKE ‘2016%‘ AND ROWNUM<10;
ELSIF SELECTION=‘2‘ THEN
OPEN REF_C FOR SELECT * FROM DJ_DJB WHERE SLBH LIKE ‘2017%‘ AND ROWNUM<10;
END IF;
LOOP
FETCH REF_C INTO V_DJB;
EXIT WHEN REF_C%NOTFOUND;
DBMS_OUTPUT.ENABLE(BUFFER_SIZE=>NULL);
DBMS_OUTPUT.PUT_LINE(V_DJB.SLBH||‘,‘||V_DJB.BDCZH);
END LOOP;
CLOSE REF_C;
END;
示例2:弱类型REF游标
DECLARE
TYPE REF_CURSOR IS REF CURSOR;
REF_C REF_CURSOR;
V_V1 VARCHAR2(100);
SELECTION VARCHAR2(1):=(‘&请输入:‘);
BEGIN
IF SELECTION=‘1‘ THEN
OPEN REF_C FOR SELECT TO_CHAR(BDCZH) FROM DJ_DJB WHERE SLBH LIKE ‘2016%‘ AND ROWNUM<10;
-------弱类型游标对目标表没有限制,数据可以使来自任何表
ELSIF SELECTION=‘2‘ THEN
OPEN REF_C FOR SELECT TO_CHAR(QLRMC) FROM DJ_QLRGL WHERE SLBH LIKE ‘2017%‘ AND ROWNUM<10 AND QLRLX=‘权利人‘;
-------弱类型游标对目标表没有限制,数据可以使来自任何表
END IF;
LOOP
FETCH REF_C INTO V_V1;
EXIT WHEN REF_C%NOTFOUND;
DBMS_OUTPUT.ENABLE(BUFFER_SIZE=>NULL);
DBMS_OUTPUT.PUT_LINE(‘输出结果值:‘||V_V1);
END LOOP;
CLOSE REF_C;
END;
示例3:SYS_REFCURSOR
DECLARE
V_DJB DJ_DJB%ROWTYPE;
REF_C SYS_REFCURSOR; ---利用SYS_REFCUSOR 来声明游标变量
SELECTION VARCHAR2(1):=(‘&请输入:‘);
BEGIN
IF SELECTION=‘1‘ THEN
OPEN REF_C FOR SELECT * FROM DJ_DJB WHERE SLBH LIKE ‘2016%‘ AND ROWNUM<10;
ELSIF SELECTION=‘2‘ THEN
OPEN REF_C FOR SELECT * FROM DJ_DJB WHERE SLBH LIKE ‘2017%‘ AND ROWNUM<11;
END IF;
LOOP
FETCH REF_C INTO V_DJB;
EXIT WHEN REF_C%NOTFOUND;
DBMS_OUTPUT.ENABLE(BUFFER_SIZE=>NULL);
DBMS_OUTPUT.PUT_LINE(V_DJB.SLBH||‘,‘||V_DJB.BDCZH);
END LOOP;
CLOSE REF_C;
END;
示例4:REF 游标作为参数传递
CREATE OR REPLACE PACKAGE EMP_T
IS
TYPE REF_CURSOR IS REF CURSOR RETURN DJ_DJB%ROWTYPE;
PROCEDURE GET_BDCZH(REF_C IN OUT REF_CURSOR,SELECTION VARCHAR2);
PROCEDURE RETURN_C(SELECTION VARCHAR2);
END;
CREATE OR REPLACE PACKAGE BODY EMP_T
IS
PROCEDURE GET_BDCZH (REF_C IN OUT REF_CURSOR,SELECTION VARCHAR2)
IS
BEGIN
IF SELECTION =1 THEN
OPEN REF_C FOR SELECT * FROM DJ_DJB WHERE SLBH LIKE ‘2016%‘ AND ROWNUM<10;
ELSIF SELECTION=2 THEN
OPEN REF_C FOR SELECT * FROM DJ_DJB WHERE SLBH LIKE ‘2017%‘ AND ROWNUM<10;
END IF;
END;
PROCEDURE RETURN_C (SELECTION VARCHAR2)
IS
V_1 REF_CURSOR;
V_DJB dj_djb%ROWTYPE;
BEGIN
GET_BDCZH(V_1,SELECTION);
LOOP
FETCH V_1 INTO V_DJB;
EXIT WHEN V_1%NOTFOUND;
DBMS_OUTPUT.ENABLE(BUFFER_SIZE=>NULL);
DBMS_OUTPUT.PUT_LINE(V_DJB.SLBH||‘,‘||V_DJB.BDCZH);
END LOOP;
CLOSE V_1;
END;
END;
---调用
begin
EMP_T.RETURN_C(1);
END;
示例5:使用BULK COLLECT INTO.......批量提取数据
DECLARE
TYPE REF_CURSOR IS REF CURSOR;
REF_C REF_CURSOR;
TYPE R_DJB IS TABLE OF DJ_DJB%ROWTYPE INDEX BY BINARY_INTEGER;
V_DJB R_DJB;
SELECTION VARCHAR2(1):=(‘&请输入:‘);
BEGIN
IF SELECTION=‘1‘ THEN
OPEN REF_C FOR SELECT * FROM DJ_DJB WHERE SLBH LIKE ‘2016%‘ AND ROWNUM<100;
ELSIF SELECTION=‘2‘ THEN
OPEN REF_C FOR SELECT * FROM DJ_DJB WHERE SLBH LIKE ‘2017%‘ AND ROWNUM<100;
END IF;
LOOP
FETCH REF_C BULK COLLECT INTO V_DJB LIMIT 10;
---EXIT WHEN REF_C%NOTFOUND;
DBMS_OUTPUT.ENABLE(BUFFER_SIZE=>NULL);
---FOR I IN 1..V_DJB.COUNT LOOP
FOR I IN V_DJB.FIRST..V_DJB.LAST LOOP
DBMS_OUTPUT.PUT_LINE(V_DJB(I).SLBH||‘,‘||V_DJB(I).BDCZH||‘I 的值为:‘||I);
END LOOP;
DBMS_OUTPUT.PUT_LINE(‘ROWCOUNT的值:‘||REF_C%ROWCOUNT);
EXIT WHEN REF_C%NOTFOUND;
END LOOP;
END;
BY WOLIAHAITO 2018.04.03
标签:SYS_REFCURSOR REF CURSOR 动态游标 ORACLE 动态游标
原文地址:http://blog.51cto.com/wolihaito/2094209