๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[database ๊ณ ๊ธ‰] ํ”„๋กœ์‹œ์ € (2023.08.09 ~ 10) ๋ณธ๋ฌธ

full stack/database

[database ๊ณ ๊ธ‰] ํ”„๋กœ์‹œ์ € (2023.08.09 ~ 10)

Shinjio 2023. 8. 9. 17:37

ํ”„๋กœ์‹œ์ €

 

SQL์„ ์ ˆ์ฐจ์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์ˆ 

SQL์€ ๋Œ€ํ‘œ์ ์ธ ๋น„์ ˆ์ฐจํ˜• ์–ธ์–ด

 

SET SERVEROUTPUT ON;

SELECT * FROM TB_CUST;

CREATE OR REPLACE PROCEDURE P_SOLD_OUT_YN_DEL
AS

v_cnt NUMBER := 0;

BEGIN

    DBMS_OUTPUT.PUT_LINE(v_cnt);
    SELECT COUNT(*) INTO v_cnt FROM TB_CUST ;
    DBMS_OUTPUT.PUT_LINE(v_cnt);

    EXCEPTION
        WHEN OTHERS THEN 
            NULL ;

END ;
/

EXEC P_SOLD_OUT_YN_DEL;

 

 

๋ณ€์ˆ˜ ์„ ์–ธ

 

 

CREATE OR REPLACE PROCEDURE P_SOLD_OUT_YN_DEL
AS
    v_cust_id TB_CUST.CUST_ID%TYPE ;
    v_cust TB_CUST%ROWTYPE;

BEGIN

    SELECT CUST_ID INTO v_cust_id FROM TB_CUST WHERE CUST_NAME = '๊ณ ๊ฐ001';
    DBMS_OUTPUT.PUT_LINE('ํ•ด๋‹น ๊ณ ๊ฐ์˜ ์•„์ด๋””๋Š” ' || v_cust_id || '์ž…๋‹ˆ๋‹ค.');
    
    SELECT * INTO v_cust FROM TB_CUST WHERE CUST_ID = 'C0001';
    DBMS_OUTPUT.PUT_LINE('ํ•ด๋‹น ๊ณ ๊ฐ์˜ ์•„์ด๋””๋Š” ' || v_cust.CUST_ID || '์ž…๋‹ˆ๋‹ค.');
    DBMS_OUTPUT.PUT_LINE('ํ•ด๋‹น ๊ณ ๊ฐ์˜ ์ƒ์ผ์€ ' || v_cust.BIRTH_DY || '์ž…๋‹ˆ๋‹ค.');

END ;

 

 

IF๋ฌธ

 

CREATE OR REPLACE PROCEDURE P_SOLD_OUT_YN_DEL
AS
    v_sold_yn VARCHAR2(5);

BEGIN

    SELECT SOLD_OUT_YN INTO v_sold_yn FROM TB_PRD WHERE PRD_ID = 'P0001';
    IF v_sold_yn = 'Y'
    THEN DBMS_OUTPUT.PUT_LINE('P0001 ์ƒํ’ˆ์€ ํ’ˆ์ ˆ์ž…๋‹ˆ๋‹ค.');
    ELSIF v_sold_yn = 'N'
    THEN DBMS_OUTPUT.PUT_LINE('P0001 ์ƒํ’ˆ์€ ํŒ๋งค์ค‘์ž…๋‹ˆ๋‹ค.');
    END IF;
    
    v_sold_yn := null;
    SELECT SOLD_OUT_YN INTO v_sold_yn FROM TB_PRD WHERE PRD_ID = 'P0015';
    IF v_sold_yn = 'Y'
    THEN DBMS_OUTPUT.PUT_LINE('P0015 ์ƒํ’ˆ์€ ํ’ˆ์ ˆ์ž…๋‹ˆ๋‹ค.');
    ELSE DBMS_OUTPUT.PUT_LINE('P0015 ์ƒํ’ˆ์€ ํŒ๋งค์ค‘์ž…๋‹ˆ๋‹ค.');
    END IF;
    
END ;
/

 

 

๋ฐ˜๋ณต๋ฌธ

 

 

CREATE OR REPLACE PROCEDURE P_SOLD_OUT_YN_DEL
AS
    v_prd_id TB_PRD.PRD_ID%TYPE;
    v_soldout_yn TB_PRD.SOLD_OUT_YN%TYPE;

CURSOR c_prd_cursor IS SELECT PRD_ID, SOLD_OUT_YN FROM TB_PRD;

BEGIN

    OPEN c_prd_cursor ;
    
    LOOP
        FETCH c_prd_cursor INTO v_prd_id, v_soldout_yn ;

    IF v_soldout_yn = 'Y' THEN
        DBMS_OUTPUT.PUT_LINE(v_prd_id || '๋Š” ํ’ˆ์ ˆ์ž…๋‹ˆ๋‹ค.');
    ELSE
        DBMS_OUTPUT.PUT_LINE(v_prd_id || '๋Š” ํŒ๋งค์ค‘์ž…๋‹ˆ๋‹ค.');
     END IF;
    
    EXIT WHEN c_prd_cursor%NOTFOUND;
  END LOOP;
  
  CLOSE c_prd_cursor;
    
END ;
/

 

 

์˜ˆ์™ธ์ฒ˜๋ฆฌ 

 

ํ”„๋กœ์‹œ์ €์—์„œ๋Š” DDL๋ฌธ์„ ๋ฐ”๋กœ ์“ธ ์ˆ˜ ์—†์Œ (commit์˜ ๊ฐœ๋…์ด ๋ชจํ˜ธํ•ด์ง )

→ execute immediate ์‚ฌ์šฉ 

 

 

๋ฐฑ์—… ํ”„๋กœ์‹œ์ €

 

 

WHERE ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์„ ์ง€์ •ํ• ์ˆ˜๋„ ์žˆ์Œ