oracle游标程序
oracle游标程序
DECLARE
CURSOR EMP_CURSOR IS
SELECT FIRST_NAME FROM EMPLOYEES;
-- TYPE NAME_ARRAY_TYPE IS varchar(5) OF VARCHAR2(10);
TYPE NAME_ARRAY_TYPE IS VARRAY(5) OF VARCHAR2(10);
NAME_ARRAY NAME_ARRAY_TYPE;
ROWS INT := 2;
V_COUNT INT := 0;
BEGIN
IF NOT EMP_CURSOR%ISOPEN -- 查看游标是否打开
THEN OPEN emp_cursor;
END IF;
-- OPEN EMP_CURSOR;
LOOP
FETCH EMP_CURSOR BULK COLLECT INTO NAME_ARRAY LIMIT ROWS; -- 如果不添加LIMIT ROWS 就去全部提取cursor的数据
DBMS_OUTPUT.PUT_LINE('############' || EMP_CURSOR%ROWCOUNT);--每次取到cursor的总数量
DBMS_OUTPUT.PUT_LINE('***********************');
FOR I IN 1 .. (EMP_CURSOR%ROWCOUNT - V_COUNT) LOOP -- 除去已经处理的cursor的数据
DBMS_OUTPUT.PUT_LINE(NAME_ARRAY(I));
END LOOP;
DBMS_OUTPUT.PUT_LINE('&&&&&&&&&&&&&&&&&&');
V_COUNT := EMP_CURSOR%ROWCOUNT;
EXIT WHEN EMP_CURSOR%NOTFOUND;
END LOOP;
CLOSE EMP_CURSOR;
END;
下面是简单cursor的处理数据:
DECLARE
CURSOR EMP_CURSOR IS
SELECT FIRST_NAME, LAST_NAME FROM EMPLOYEES;
V_FIRSTNAME EMPLOYEES.FIRST_NAME%TYPE;
V_LASTNAME EMPLOYEES.LAST_NAME%TYPE;
BEGIN
OPEN EMP_CURSOR;
LOOP
FETCH EMP_CURSOR
INTO V_FIRSTNAME, V_LASTNAME;
EXIT WHEN EMP_CURSOR%NOTFOUND;
DBMS_OUTPUT.PUT_line(V_FIRSTNAME ||':' || V_LASTNAME);
END LOOP;
CLOSE EMP_CURSOR;
END;