PL/SQL + IF문
PL/SQL 문법
-- PL/SQL : 오라클에서 만든 비즈니스 로직작업을 하기 위한 데이터베이스 프로그래밍 언어
-- SQL 포함하는 의미
SET SERVEROUTPUT ON;
-- 익명블록
-- 선언부
DECLARE
VI_NUM NUMBER; -- 변수선언(정의)
-- 실행부
BEGIN
VI_NUM := 100; -- := 초기값, 초기값 없으면 NULL
DBMS_OUTPUT.PUT_LINE(VI_NUM); -- Syetem.out.println()와 유사
END;
-- 상수명 CONSTANT 데이터타입 := 상수값;
-- 상수 선언시 반드시 초기화. 값 변경X
-- 2의 3제곱 2**#
DECLARE
a INTEGER := 2**2*3**2;
BEGIN
DBMS_OUTPUT.PUT_LINE('a = ' || TO_CHAR(a));
END;
-- 사원테이블에서 특정 사원의 이름과 부서명을 변수에 저장하고, 출력하는 예제
-- SQL 구문
SELECT DEPARTMENT_NAME, EMP_NAME
FROM EMPLOYEES A, DEPARTMENTS B
WHERE A.DEPARTMENT_ID = B.DEPARTMENT_ID
AND A.EMPLOYEE_ID = 100;
-- PL/SQL 구문
DECLARE
VS_EMP_NAME VARCHAR2(80);
VS_DEP_NAME VARCHAR2(80);
BEGIN
SELECT EMP_NAME, DEPARTMENT_NAME
INTO VS_EMP_NAME, VS_DEP_NAME -- 두 변수의 컬럼의 값이 저장된다
FROM EMPLOYEES A, DEPARTMENTS B
WHERE A.DEPARTMENT_ID = B.DEPARTMENT_ID
AND A.EMPLOYEE_ID = 100;
DBMS_OUTPUT.PUT_LINE(VS_EMP_NAME || ' - ' || VS_DEP_NAME);
END;
SQL과 PL/SQL 데이터 타입별 길이
-- SQL과 PL/SQL 데이터 타입별 길이
-- 예를 들어, VARCHAR2 타입은 SQL에서는 최대 크기가 4000 byte였다.
-- 하지만 PL/SQL에서는 VARCHAR2 타입을 32KB(32, 767 byte)까지 사용할 수 있다.
-- 1) 4000 OVER 에러발생
CREATE TABLE CH08_VARCHAR2 (
VAR1 VARCHAR2(5000)
);
CREATE TABLE CH08_VARCHAR2 (
VAR1 VARCHAR2(4000)
);
-- 데이터 넣기
INSERT INTO ch08_varchar2 (VAR1)
VALUES ('tQbADHDjqtRCvosYCLwzbyKKrQCdJubDPTHnzqvjRwGxhQJtrVbXsLNlgeeMCemGMYpvfoHUHDxIPTDjleABGoowxlzCVipeVwsMFRNzZYgHfQUSIeOITaCKJpxAWwydApVUlQiKDgJlFIOGPOKoJsoemqNbOLdZOBcQhDcMLXuYjRQZDIpgpmImgiwzcLkSilCmLrSbmFNsKEEpzCHDylMvkYPKPNeuJxLvJiApNCYzrMcflECbxwNTKSxaEwVvCYnTnFfMFgDqxobWcSmMJrNTQIVOeWlPaMTfRHsrlFSukppmljmOojPSgJiSbQcgtWWOwUNNYFGtgCGBsIcTGAiHWBxtYVXecoJgJCAJptIVmVTZSKliRLoPYTIUpksBuQaqFHLhCkosWChoMjbqgLtBIRBynsKjKiLrdeHVvZanNVElDjLWwlCDhbpsAVQMTzjzhoKIJBdthynMBMVjeNmsKAjdAYhPZKmuKOuMloQdkqPjoKbfjDEeATciMrXiMQorMhYmBlMODBbyLLIkbmtZdPcWGSuxFEUwXnWpvnunEgcLelSneRIpgRNTzTkHqgLbpxoHzCYgSWlIAvKljCnmWiPWGGwlUFOudRSdoqUxntyhNYEiVXtMObywEltTImawnElpmeiWwlTjGTFceqyjhNqiDLxwduubykWzDmFSJNvVvDZibrCpAReqQjlQZcxuVqjKGKvoDuEcQPQeDzmdMYSOTIQdPDNfDffCOUWflHSQhvVTiYumBQIoyznWNITGZkefknJpGEutUnhBgLPQTWTBeTYccqlLrxvRjfJpdpfVDqqfKCngemIEDDHNdvBxCqKDTrrJAumXMKgpWLIHctQuACeNaKnffpYXiioLxZDrxpuZPPUGpRsCtoQuBfogkKuusVATkMyajKTPSyTQbfhZepRjNdrhkymqKvsAcThYbMSMnkKcLWFPAMeGysBVKkQtFMPvRBoDszlSZcMYzwxkKQwJnuVnDxShYiHFlzgDWqhZoqeypyFVBNDtHkiVzHkQisYLbsbVneJyHbHdtaIF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VXjDJXzOiszXQCErQwwDMMqjLxWebJwNAVdrXeyMDRYXmLMDnuWLVaShVGhlgvbjOdOnhCDTNVazYDnzstqxjOuWbLcDaavRumKUOQXBQwKtdFgOzXiQKWFporrIcylIHlTmTKAIpBqNUbkajLTlwAHieCcqPIJYhegwQhWpYZdfxpQXDKtYzsrmnvdiTKgXfXKlIHPHlxQtqXGhMVPOBAKVZJfkrDNEwnQFwgfoHJSqQxTzRswVLrtFgpVzKcLilgznElWUfhERyeUrCcFCuGJddlFHJrXsqRdUjqUwaBmJVNwjRbCFiVMOSFuNctNVzhmhUpoddsMPUFMvNIMsMjHIWYiLjhSajZqpDkMvUOUCbYKfNHGpdUeWGUtDXHDNSCEXqYrhWhvnISnjfoBMCwwptksarPImRZaRxBMjoBdlmRGlIuQZDzCLnxxioATnGVFFTATUpeypOCaCeJAvPLxEXYzlCgXvXirGSZFyZPPSCdOSHxeELRsetFrWgqPNNpwgbgBEYPOSpLWeVdqOxPaQnidyPVMmELzeJPWgNsWBdPJPjhkdGpeAYZfrBNqdbOwzbtLiWMPafjgWQNcWKqmcleWLcMJoGSAEIUyFuzElZKXonHOMDdGMtSKEFUWdfPfnDecKNhIjAKRYmkXgpPAzlKIOpViZPkZdozzAoWwDnXkfDikvkXcQaoBtzKkcRhNpJRYaGTkdnlfotsJZsLqpYaWoK')
;
COMMIT;
SELECT * FROM CH08_VARCHAR2;
DECLARE
vs_sql_varchar2 VARCHAR2(4000);
vs_plsql_varchar2 VARCHAR2(32767);
BEGIN
-- ch08_varchar2 테이블의 값을 변수에 담는다.
SELECT VAR1
INTO vs_sql_varchar2
FROM ch08_varchar2;
-- PL/SQL 변수에 4000 BYTE 이상 크기의 값을 넣는다.
vs_plsql_varchar2 := vs_sql_varchar2 || ' - ' || vs_sql_varchar2 || ' - ' || vs_sql_varchar2;
-- 각 변수 크기를 출력한다.
DBMS_OUTPUT.PUT_LINE('SQL VARCHAR2 길이 : ' || LENGTHB(vs_sql_varchar2));
DBMS_OUTPUT.PUT_LINE('PL/SQL VARCHAR2 길이 : ' || LENGTHB(vs_plsql_varchar2));
END;
PL/SQL IF 문
DECLARE
VN_NUM1 NUMBER := 1;
VN_NUM2 NUMBER := 2;
BEGIN
IF VN_NUM1 >= VN_NUM1 THEN
DBMS_OUTPUT.PUT_LINE(VN_NUM1 || '이 큰수');
ELSE
DBMS_OUTPUT.PUT_LINE(VN_NUM2 || '이 큰수');
END IF;
END;
-- DBMS_RANDOM.VALUE(10, 120) : 10~120범위의 실수갑을 무작위로 반환
SELECT DBMS_RANDOM.VALUE(10, 120), ROUND(DBMS_RANDOM.VALUE(10, 120), -1) FROM DUAL;
DECLARE
VN_SALARY NUMBER := 0;
VN_DEPARTMENT_ID NUMBER := 0;
BEGIN
VN_DEPARTMENT_ID := ROUND(DBMS_RANDOM.VALUE(10, 120), -1);
SELECT SALARY
INTO VN_SALARY
FROM EMPLOYEES
WHERE DEPARTMENT_ID = VN_DEPARTMENT_ID
AND ROWNUM = 1;
DBMS_OUTPUT.PUT_LINE(VN_SALARY);
IF VN_SALARY BETWEEN 1 AND 3000 THEN
DBMS_OUTPUT.PUT_LINE('낮음');
ELSIF VN_SALARY BETWEEN 3001 AND 6000 THEN
DBMS_OUTPUT.PUT_LINE('중간');
ELSIF VN_SALARY BETWEEN 6001 AND 10000 THEN
DBMS_OUTPUT.PUT_LINE('높음');
ELSE
DBMS_OUTPUT.PUT_LINE('최상위');
END IF;
END;
예제
-- 사원 테이블에서 커미션까지 가져와 커미션이 0보다 클 경우,
-- 다시 조건을 걸어 커미션이 0.15보다 크면 ‘급여*커미션’ 결과를 출력하고, 커미션이 0보다 작으면 급여만 출력
SET SERVEROUTPUT ON;
-- 익명블록
DECLARE
VN_SALARY NUMBER := 0;
VN_DEPARTMENT_ID NUMBER := 0;
VN_COMMISSION NUMBER := 0;
BEGIN
-- 랜덤으로 10~120범위의 부서코드
VN_DEPARTMENT_ID := ROUND(dbms_random.value(10, 120), -1);
SELECT SALARY, COMMISSION_PCT
INTO VN_SALARY, VN_COMMISSION
FROM EMPLOYEES
WHERE DEPARTMENT_ID = VN_DEPARTMENT_ID
AND ROWNUM = 1;
DBMS_OUTPUT.PUT_LINE(VN_SALARY);
-- 중첩 IF문
IF VN_COMMISSION > 0 THEN
IF VN_COMMISSION > 0.15 THEN
DBMS_OUTPUT.PUT_LINE(VN_SALARY * VN_COMMISSION);
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE(VN_SALARY);
END IF;
END;
댓글남기기