1. 기본적 커서 구문

CURSOR cursor_name 
IS
SELECT ...

OPEN cursor_name;
LOOP
        FETCH cursor_name INTO ....;

        EXIT WHEN cursor_name%NOTFOUND;

END LOOP;
CLOSE cursor_name;

 

2. fetch 구문 없는 커서

CURSOR cursor_name 
IS
SELECT ...

FOR v_row IN cursor_name;
LOOP

        EXIT WHEN cursor_name%NOTFOUND;

END LOOP;

 

3. 매개변수 있는 커서 활용

CURSOR cursor_name(v_uid NUMBER)
IS
SELECT ...
FROM ..
WHERE uid = v_uid;

FOR v_row IN cursor_name(10);
LOOP

        EXIT WHEN cursor_name%NOTFOUND;

END LOOP;

 

4. 선언없는 커서

FOR v_row IN (SELECT ... FROM ...)
LOOP

END LOOP;

 

5. 동적쿼리 커서

TYPE RTN_CURSOR IS REF CURSOR;
cursor_name RTN_CURSOR;

OPEN cursor_name FOR 'SELECT NVL(MAX(' || V_COLUMN_NAME || '), 0) + 1 FROM ' || V_TABLE_NAME;
LOOP
        FETCH cursor_name INTO ....;

        EXIT WHEN cursor_name%NOTFOUND;

END LOOP;
CLOSE cursor_name;

 

6. 루프

DECLARE

        V_LENGTH        NUMBER;

BEGIN

        V_LENGTH        := 10;

        FOR V_IDX IN 1 .. V_LENGTH
        LOOP

                DBMS_OUTPUT.PUT_LINE(TO_CHAR(V_IDX));

        END LOOP;
END;

 


-- 예제 (fetch 구문 없는 커서)

DECLARE
    CURSOR CURSOR_NAME 
    IS
    SELECT LEVEL AS NO
    FROM DUAL
    CONNECT BY LEVEL <= 10;
    
BEGIN

    FOR V_ROW IN CURSOR_NAME
    LOOP
        
        IF V_ROW.NO = 5 THEN CONTINUE; END IF;
        
        V_ROW.NO := V_ROW.NO + 10;
        DBMS_OUTPUT.PUT_LINE(TO_CHAR(V_ROW.NO));

    END LOOP;
END;


-- 결과화면
-------------------------------
11
12
13
14
16
17
18
19
20