SET VERIFY OFF
SET SERVEROUTPUT ON -- 화면에 출력하고 싶을 때 사용
ACCEPT p_deptno PROMPT '부서번호를 입력하시오:' -- 입력
DECLARE -- 선언부
v_sal_total NUMBER;
BEGIN -- 구현부
SELECT SUM(SALARY) INTO v_sal_total
FROM EMPLOYEES
WHERE DEPARTMENT_ID = &p_deptno;
DBMS_OUTPUT.PUT_LINE(&p_deptno || '번 부서의 급여의 합' ||
LTRIM(TO_CHAR(v_sal_total, '$999,999,999')) );
END;
/
SET VERIFY ON
SET SERVEROUTPUT OFF
-- 선언부
-- 실행부
-- 예외처리부
/*
DECLARE -- 선택
선언
BEGIN -- 필수
구현
EXCEPTION -- 선택
예외
END;
/
*/
-- 콘솔 출력(확인)
-- String v_name = "홍길동";
-- SQL 스크립트
SET SERVEROUTPUT ON;
DECLARE
V_NAME VARCHAR2(30) := 'PL';
BEGIN
DBMS_OUTPUT.PUT_LINE('오라클과 ' || V_NAME);
END;
/
SET SERVEROUTPUT OFF;
-- 반복문(1부터 10까지 더하기) -- LOOP
SET SERVEROUTPUT ON;
DECLARE
V_CNT NUMBER := 0;
V_TOT NUMBER := 0;
BEGIN
LOOP
EXIT WHEN V_CNT = 10;
V_CNT := V_CNT + 1;
V_TOT := V_TOT + V_CNT;
DBMS_OUTPUT.PUT_LINE('V_CNT : ' || V_CNT);
END LOOP;
DBMS_OUTPUT.PUT_LINE('V_TOT : ' || V_TOT);
END;
/
SET SERVEROUTPUT OFF;
-- WHILE
SET SERVEROUTPUT ON;
DECLARE
V_CNT NUMBER := 0;
V_TOT NUMBER := 0;
BEGIN
WHILE V_CNT < 10
LOOP
V_CNT := V_CNT + 1;
V_TOT := V_TOT + V_CNT;
DBMS_OUTPUT.PUT_LINE('V_CNT : ' || V_CNT);
END LOOP;
DBMS_OUTPUT.PUT_LINE('V_TOT : ' || V_TOT);
END;
/
SET SERVEROUTPUT OFF;
-- FOR
SET SERVEROUTPUT ON;
DECLARE
V_CNT NUMBER := 0;
V_TOT NUMBER := 0;
BEGIN
FOR V_CNT IN 1 .. 10
LOOP
V_TOT := V_TOT + V_CNT;
DBMS_OUTPUT.PUT_LINE('V_CNT : ' || V_CNT);
END LOOP;
DBMS_OUTPUT.PUT_LINE('V_TOT : ' || V_TOT);
END;
/
SET SERVEROUTPUT OFF;
-- IF
SET SERVEROUTPUT ON;
DECLARE
V_CNT NUMBER := 0;
V_TOT NUMBER := 0;
BEGIN
FOR V_CNT IN 1 .. 10
LOOP
IF MOD(V_CNT, 3) = 0 THEN
V_TOT := V_TOT + V_CNT;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('V_TOT : ' || V_TOT);
END;
/
SET SERVEROUTPUT OFF;
-- CASE 와 GOTO
SET SERVEROUTPUT ON;
DECLARE
V_NAME VARCHAR2(30) := 'LEE';
V_CASE NUMBER := 1;
BEGIN
CASE WHEN MOD(V_CASE, 2) = 0 THEN
GOTO TEST1;
WHEN MOD(V_CASE, 2) = 1 THEN
GOTO TEST2;
ELSE
GOTO ERR;
END CASE;
<<TEST1>>
DBMS_OUTPUT.PUT_LINE(V_NAME || ' IS MAN');
GOTO SUB_END;
<<TEST2>>
DBMS_OUTPUT.PUT_LINE(V_NAME || ' IS WOMAN');
GOTO SUB_END;
<<ERR>>
DBMS_OUTPUT.PUT_LINE('ERR NAME : ' || V_NAME);
GOTO SUB_END;
<<SUB_END>>
DBMS_OUTPUT.PUT_LINE('EXIT SUB');
END;
/
SET SERVEROUTPUT OFF;
'Oracle Database > SQL' 카테고리의 다른 글
[ORACLE] DB의 PL/SQL (0) | 2018.06.28 |
---|---|
SQL PL(2) (0) | 2018.06.28 |
SQL index (0) | 2018.06.27 |
SQL sequence (0) | 2018.06.27 |
SQL 정리해라 (0) | 2018.06.27 |