Work

Cursor 예문

runicode 2009. 4. 29. 16:34
출처는 : http://kin.naver.com/detail/detail.php?d1id=1&dir_id=10110&eid=N5I1ruz6FGn7B3LLNcadbTFvjbLJu0cU&qb=Q1VSU09SIGVtcF9jdXJzb3IgSVM=&enc=utf8&pid=fQlt%2Bwoi5UNssvT3PZGsss--480430&sid=SfgB@9L190kAAG2TogA

문제) EMP 테이블에서 부서번호와 업무를 입력받아 사원번호, 이름, 급여를
출력하는 SCRIPT를 작성하라!!

set verify off
set serveroutput on

ACCEPT p_deptno PROMPT '부서번호를 입력하라 : '
ACCEPT p_job PROMPT '담당업무를 입력하라 : '

DECLARE
TYPE emp_record_type IS RECORD(
v_empno emp.empno%TYPE,
v_ename emp.ename%TYPE,
v_sal emp.sal%TYPE);

emp_record emp_record_type;
v_sal_total NUMBER(10,2) := 0;

CURSOR emp_cursor(v_deptno emp.deptno%TYPE, v_job VARCHAR2) IS

SELECT empno, ename, sal
FROM emp
WHERE deptno = v_deptno AND job = v_job
ORDER BY empno;

BEGIN
DBMS_OUTPUT.PUT_LINE('사 번 이 름 급 여');
DBMS_OUTPUT.PUT_LINE('----- ------- -----------');

OPEN emp_cursor(&p_deptno, UPPER('&p_job'));

LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
v_sal_total := v_sal_total + emp_record.v_sal;
DBMS_OUTPUT.PUT_LINE(RPAD(emp_record.v_empno,6)||RPAD(emp_record.v_ename,12)
||LPAD(TO_CHAR(emp_record.v_sal,'$99,999,990.00'),16));
END LOOP;

DBMS_OUTPUT.PUT_LINE('-----------------------------');
DBMS_OUTPUT.PUT_LINE(RPAD(TO_CHAR(&p_deptno),2)||'번 부서의 합'
||LPAD(TO_CHAR(v_sal_total,'$99,999,990.00'),16));

CLOSE emp_cursor;
END;
/

set verify on
set serveroutput off