PL/SQL 커서

June Lee·2021년 2월 23일

PL/SQL

목록 보기
3/7

PL/SQL에서 질의 결과가 다중 로우일 경우 반드시 커서를 사용해야한다.

1) LOOP 사용

DECLARE
	CURSOR department_cursors IS
    	SELECT department_id, department_name, location_id
        	FROM departments;
    department_record department_cursors%ROWTYPE;

BEGIN
	--커서 열기
    OPEN department_cursors;
    LOOP
    	FETCH department_cursors
        	INTO department_record;
        EXIT WHEN department_cursors%NOTFOUND;
        dbms_output.put_line(department_record.department_id || ' ' || department_record.department_name || ' ' || department_record.location_id);
    END LOOP;
    dbms_output.put_line(department_cursors%rowcount || '개의 행이 검색되었습니다.'); --LOOP 쓸 때만 가능
    CLOSE department_cursors;

END;

2) FOR문 사용

DECLARE
	CURSOR department_cursors IS
    	SELECT department_id, department_name, location_id
        	FROM departments;
    department_record department_cursors%ROWTYPE;

BEGIN
	FOR department_record IN department_cursors LOOP
    	dbms_output.put_line(department_record.department_id || ' ' || department_record.department_name || ' ' || department_record.location_id);
    END LOOP;

END;
profile
📝 dev wiki

0개의 댓글