[Day14] Oracle14 [10/17]

 

[๋ณต์Šต๋ฌธ์ œ]
------------------------------------------------------------------------------------
T_MEMBER ํšŒ์›
T_POLL   ์„ค๋ฌธ
T_POLLSUB ์„ค๋ฌธํ•ญ๋ชฉ
T_VOTER ํˆฌํ‘œ

 

1) ์„ค๋ฌธํ…Œ์ด๋ธ” ์ƒ์„ฑ

CREATE TABLE SCOTT.T_POLL (
   PollSeq NUMBER(4) NOT NULL,
   Question Varchar(256) NOT NULL,
   SDate DATE NOT NULL,
   EDAte DATE NOT NULL,
   ItemCount Number(1) DEFAULT 1 NOT NULL,
   PollTotal NUMBER(4),
   RegDate Date DEFAULT sysdate NOT NULL,
   MemberSEQ NUMBER(4)
)

 

COMMENT ON TABLE SCOTT.T_POLL IS '์„ค๋ฌธ์กฐ์‚ฌ'
COMMENT ON COLUMN SCOTT.T_POLL.PollSeq IS '์„ค๋ฌธSEQ'
COMMENT ON COLUMN SCOTT.T_POLL.Question IS '์งˆ๋ฌธ'
COMMENT ON COLUMN SCOTT.T_POLL.SDate IS '์‹œ์ž‘์ผ'
COMMENT ON COLUMN SCOTT.T_POLL.EDAte IS '์ข…๋ฃŒ์ผ'
COMMENT ON COLUMN SCOTT.T_POLL.ItemCount IS '๋‹ต๋ณ€ํ•ญ๋ชฉ์ˆ˜'
COMMENT ON COLUMN SCOTT.T_POLL.PollTotal IS '์ด์ฐธ์—ฌ์ˆ˜'
COMMENT ON COLUMN SCOTT.T_POLL.RegDate IS '์ž‘์„ฑ์ผ'
COMMENT ON COLUMN SCOTT.T_POLL.MemberSEQ IS '์ž‘์„ฑ์ž(ํšŒ์›SEQ)'
CREATE UNIQUE INDEX SCOTT.PK_T_POLL
   ON SCOTT.T_POLL (
      PollSeq ASC
   )
ALTER TABLE SCOTT.T_POLL
   ADD
      CONSTRAINT PK_T_POLL
      PRIMARY KEY (
         PollSeq
      )


2) ์„ค๋ฌธํ•ญ๋ชฉ ํ…Œ์ด๋ธ” ์ƒ์„ฑ

CREATE TABLE SCOTT.T_PollSub (
   PollSubSeq NUMBER(38) NOT NULL,
   Answer Varchar2(100) NOT NULL,
   ACount Number(4),
   PollSeq NUMBER(4) NOT NULL
)
COMMENT ON TABLE SCOTT.T_PollSub IS '์„ค๋ฌธํ•ญ๋ชฉ'
COMMENT ON COLUMN SCOTT.T_PollSub.PollSubSeq IS '๋‹ต๋ณ€ํ•ญ๋ชฉSEQ'
COMMENT ON COLUMN SCOTT.T_PollSub.Answer IS '๋‹ต๋ณ€ํ•ญ๋ชฉ'
COMMENT ON COLUMN SCOTT.T_PollSub.ACount IS '๋‹ต๋ณ€ํ•ญ๋ชฉ์„ ํƒ์ˆ˜'
COMMENT ON COLUMN SCOTT.T_PollSub.PollSeq IS '์„ค๋ฌธSEQ'
CREATE UNIQUE INDEX SCOTT.PK_T_PollSub
   ON SCOTT.T_PollSub (
      PollSubSeq ASC
   )
ALTER TABLE SCOTT.T_PollSub
   ADD
      CONSTRAINT PK_T_PollSub
      PRIMARY KEY (
         PollSubSeq
      )




3) ํˆฌํ‘œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ

CREATE TABLE SCOTT.T_Voter (
   VectorSeq NUMBER NOT NULL,
   UserName VARCHAR2(20),
   RegDate DATE,
   PollSeq NUMBER(4),
   PollSubSeq NUMBER(38),
   MemberSeq NUMBER(4)
)
COMMENT ON TABLE SCOTT.T_Voter IS 'ํˆฌํ‘œ์ž'
COMMENT ON COLUMN SCOTT.T_Voter.VectorSeq IS 'ํˆฌํ‘œSEQ'
COMMENT ON COLUMN SCOTT.T_Voter.UserName IS '์‚ฌ์šฉ์ž์ด๋ฆ„'
COMMENT ON COLUMN SCOTT.T_Voter.RegDate IS 'ํˆฌํ‘œ์ผ'
COMMENT ON COLUMN SCOTT.T_Voter.PollSeq IS '์„ค๋ฌธSEQ'
COMMENT ON COLUMN SCOTT.T_Voter.PollSubSeq IS '๋‹ต๋ณ€ํ•ญ๋ชฉSEQ'
COMMENT ON COLUMN SCOTT.T_Voter.MemberSeq IS 'ํšŒ์›SEQ'
CREATE UNIQUE INDEX SCOTT.PK_T_Voter
   ON SCOTT.T_Voter (
      VectorSeq ASC
   )
ALTER TABLE SCOTT.T_Voter
   ADD
      CONSTRAINT PK_T_Voter
      PRIMARY KEY (
         VectorSeq
      )



4) ํšŒ์› ํ…Œ์ด๋ธ” ์ƒ์„ฑ

CREATE TABLE SCOTT.T_Member (
   MemberSeq NUMBER(4) NOT NULL,
   MemberID varchar2(20) NOT NULL,
   MemberPasswd varchar2(20),
   MemberName varchar2(20),
   MemberPhone varchar2(20),
   MemberAddress varchar2(100)
)
COMMENT ON TABLE SCOTT.T_Member IS 'ํšŒ์›'
COMMENT ON COLUMN SCOTT.T_Member.MemberSeq IS 'ํšŒ์›SEQ'
COMMENT ON COLUMN SCOTT.T_Member.MemberID IS 'ํšŒ์›์•„์ด๋””'
COMMENT ON COLUMN SCOTT.T_Member.MemberPasswd IS '๋น„๋ฐ€๋ฒˆํ˜ธ'
COMMENT ON COLUMN SCOTT.T_Member.MemberName IS 'ํšŒ์›๋ช…'
COMMENT ON COLUMN SCOTT.T_Member.MemberPhone IS 'ํœด๋Œ€ํฐ'
COMMENT ON COLUMN SCOTT.T_Member.MemberAddress IS '์ฃผ์†Œ'
CREATE UNIQUE INDEX SCOTT.PK_T_Member
   ON SCOTT.T_Member (
      MemberSeq ASC
   )
ALTER TABLE SCOTT.T_Member
   ADD
      CONSTRAINT PK_T_Member
      PRIMARY KEY (
         MemberSeq
      )
ALTER TABLE SCOTT.T_POLL
   ADD
      CONSTRAINT FK_T_Member_TO_T_POLL
      FOREIGN KEY (
         MemberSEQ
      )
      REFERENCES SCOTT.T_Member (
         MemberSeq
      )
ALTER TABLE SCOTT.T_PollSub
   ADD
      CONSTRAINT FK_T_POLL_TO_T_PollSub
      FOREIGN KEY (
         PollSeq
      )
      REFERENCES SCOTT.T_POLL (
         PollSeq
      )
ALTER TABLE SCOTT.T_Voter
   ADD
      CONSTRAINT FK_T_POLL_TO_T_Voter
      FOREIGN KEY (
         PollSeq
      )
      REFERENCES SCOTT.T_POLL (
         PollSeq
      )
ALTER TABLE SCOTT.T_Voter
   ADD
      CONSTRAINT FK_T_PollSub_TO_T_Voter
      FOREIGN KEY (
         PollSubSeq
      )
      REFERENCES SCOTT.T_PollSub (
         PollSubSeq
      )
ALTER TABLE SCOTT.T_Voter
   ADD
      CONSTRAINT FK_T_Member_TO_T_Voter
      FOREIGN KEY (
         MemberSeq
      )
      REFERENCES SCOTT.T_Member (
         MemberSeq
      )


------------------------------------------------------------------------------------
ํšŒ์›

1) ํšŒ์› ๊ฐ€์ž…/ํƒˆํ‡ด/์ˆ˜์ •
CREATE SEQUENCE seq_member
INCREMENT BY 1
START WITH 1
MAXVALUE   9999
NOCACHE;
INSERT INTO T_MEMBER ( MemberSeq, MemberID, MemberPasswd, MemberName,MemberPhone, MemberAddress  )
VALUES ( seq_member.nextval,'admin', '1234', '๊ด€๋ฆฌ์ž','010-1111-1111', '์„œ์šธ ๊ฐ•๋‚จ๊ตฌ'  );
INSERT INTO T_MEMBER ( MemberSeq, MemberID, MemberPasswd, MemberName,MemberPhone, MemberAddress  )
VALUES ( seq_member.nextval,'hong', '1234', 'ํ™๊ธธ๋™','010-2222-2222', '๊ฒฝ๊ธฐ๋„ ๋‚จ์–‘์ฃผ'  );
INSERT INTO T_MEMBER ( MemberSeq, MemberID, MemberPasswd, MemberName,MemberPhone, MemberAddress  )
VALUES ( seq_member.nextval,'kim', '1234', '๊น€๊ธฐ์ˆ˜','010-3333-3333', '์„œ์šธ ์–‘์ฒœ๊ตฌ'  );
COMMIT;


2) ์„ค๋ฌธ ๋“ฑ๋ก(์ž‘์„ฑ) / ์ˆ˜์ • / ํƒˆํ‡ด

CREATE SEQUENCE seq_poll;
-- Sequence SEQ_POLL์ด(๊ฐ€) ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
INSERT INTO T_POLL (PollSeq,Question,SDate, EDAte , ItemCount,PollTotal, RegDate, MemberSEQ )
VALUES             ( seq_poll.nextval,'์ข‹์•„ํ•˜๋Š” ์—ฌ๋ฐฐ์šฐ?'
                          , TO_DATE( '2022-03-01 00:00:00'   ,'YYYY-MM-DD HH24:MI:SS')
                          , TO_DATE( '2022-03-15 18:00:00'   ,'YYYY-MM-DD HH24:MI:SS') 
                          , 5
                          , 0
                          , TO_DATE( '2022-02-01 00:00:00'   ,'YYYY-MM-DD HH24:MI:SS')
                          , 1
                    );
INSERT INTO T_POLL (PollSeq,Question,SDate, EDAte , ItemCount,PollTotal, RegDate, MemberSEQ )
VALUES             ( seq_poll.nextval,'์ข‹์•„ํ•˜๋Š” ๊ณผ๋ชฉ?'
                          , TO_DATE( '2022-04-20 00:00:00'   ,'YYYY-MM-DD HH24:MI:SS')
                          , TO_DATE( '2022-05-01 18:00:00'   ,'YYYY-MM-DD HH24:MI:SS') 
                          , 4
                          , 0
                          , TO_DATE( '2022-04-15 00:00:00'   ,'YYYY-MM-DD HH24:MI:SS')
                          , 1
                    );
INSERT INTO T_POLL (PollSeq,Question,SDate, EDAte , ItemCount,PollTotal, RegDate, MemberSEQ )
VALUES             ( seq_poll.nextval,'5์›” 5์ผ ํœด๊ฐ• ์ฐฌ๋ฐ˜?'
                          , TO_DATE( '2022-05-01 00:00:00'   ,'YYYY-MM-DD HH24:MI:SS')
                          , TO_DATE( '2022-05-04 18:00:00'   ,'YYYY-MM-DD HH24:MI:SS') 
                          , 2
                          , 0
                          , SYSDATE
                          , 1
                    );
COMMIT;




-- ์„ธ๋ถ€ ํ•ญ๋ชฉ ์ถ”๊ฐ€ ์ฟผ๋ฆฌ ์ž‘์„ฑ  

CREATE SEQUENCE seq_pollsub;
INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'๋ฐฐ์Šฌ๊ธฐ', 0, 1 );
INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'๊น€์˜ฅ๋นˆ', 0, 1 );
INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'์•„์ด์œ ', 0, 1 );
INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'๊น€์„ ์•„', 0, 1 );

INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'์ˆ˜ํ•™', 0, 2 );
INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'๊ตญ์–ด', 0, 2 );
INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'์˜์–ด', 0, 2 );
INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'์‚ฌํšŒ', 0, 2 );
INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'๊ณผํ•™', 0, 2 );


INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'์˜ˆ', 0, 3 );
INSERT INTO T_PollSub (PollSubSeq          , Answer , ACount , PollSeq  ) 
VALUES                (seq_pollsub.nextval ,'์•„๋‹ˆ์š”', 0, 3 );

COMMIT;



-- ์„ค๋ฌธ ์ˆ˜์ •/์‚ญ์ œ 

3) ์„ค๋ฌธ ๋ชฉ๋ก ํŽ˜์ด์ง€ = ์ฟผ๋ฆฌ
-- ๋ฒˆํ˜ธ/์งˆ๋ฌธ/์ž‘์„ฑ์ž/์‹œ์ž‘์ผ/์ข…๋ฃŒ์ผ/ํ•ญ๋ชฉ์ˆ˜/์ฐธ์—ฌ์ˆ˜/์ƒํƒœ

SELECT pollseq, question, membername , sDate, edate
       , itemcount, polltotal
       , CASE 
            WHEN SYSDATE > edate THEN  '์ข…๋ฃŒ'
            WHEN SYSDATE BETWEEN sdate AND edate THEN  '์ง„ํ–‰์ค‘'
            ELSE   '์‹œ์ž‘์ „'
         END  state
FROM t_poll  p JOIN t_member m ON p.memberseq = m.memberseq ;



4) [์„ค๋ฌธ ํˆฌํ‘œ] / ์ˆ˜์ • / ์‚ญ์ œ
-- [์„ค๋ฌธ ํˆฌํ‘œ]   1) + 2) + 3)    PL/SQL 

-- 1)
INSERT INTO t_voter (VectorSeq         , UserName,RegDate, PollSeq,PollSubSeq, MemberSeq) 
VALUES             ( seq_vecter.nextval, 'ํ™๊ธธ๋™', SYSDATE,  1,     3             , 2 );
-- 2)
UPDATE t_poll
SET polltotal = polltotal +1
WHERE pollseq = 1;
--3)
UPDATE t_pollsub
SET acount = acount + 1
WHERE pollsubseq = 3;


-- [์„ค๋ฌธ ํˆฌํ‘œ]   1) + 2) + 3)    PL/SQL 
-- 1)

INSERT INTO t_voter (VectorSeq         , UserName,RegDate, PollSeq,PollSubSeq, MemberSeq) 
VALUES             ( seq_vecter.nextval, '๊น€๊ธฐ์ˆ˜', SYSDATE,  1,     2             , 3 );
-- 2)
UPDATE t_poll
SET polltotal = polltotal +1
WHERE pollseq = 1;
--3)
UPDATE t_pollsub
SET acount = acount + 1
WHERE pollsubseq = 3;



-- [์„ค๋ฌธ ํˆฌํ‘œ]   1) + 2) + 3)    PL/SQL ํŠธ๋ฆฌ๊ฑฐ( Trigger)

-- 1)
INSERT INTO t_voter (VectorSeq         , UserName,RegDate, PollSeq,PollSubSeq, MemberSeq) 
VALUES             ( seq_vecter.nextval, '๊ด€๋ฆฌ์ž', SYSDATE,  1,     2             , 1 );
-- 2)
UPDATE t_poll
SET polltotal = polltotal +1
WHERE pollseq = 1;
--3)
UPDATE t_pollsub
SET acount = acount + 1
WHERE pollsubseq = 2;



5) 1๋ฒˆ ์„ค๋ฌธ์— ๋Œ€ํ•œ  ํˆฌํ‘œ ๊ฒฐ๊ณผ ๋ณด๊ธฐ

-- 1) 
SELECT COUNT(*)
FROM t_voter
WHERE pollseq = 1;
-- 1-2)  polltotal ์ฐธ์—ฌ์ž์ˆ˜
SELECT polltotal
FROM t_poll
WHERE pollseq = 1;
-- 1) 
SELECT COUNT(*)
FROM t_voter
WHERE pollseq = 1;
-- 1-2)  polltotal ์ฐธ์—ฌ์ž์ˆ˜
SELECT polltotal
FROM t_poll
WHERE pollseq = 1;

 

์ข‹์•„ํ•˜๋Š” ์—ฌ๋ฐฐ์šฐ?   ๋ฐฐ์Šฌ๊ธฐ    0(0%)
์ข‹์•„ํ•˜๋Š” ์—ฌ๋ฐฐ์šฐ?   ๊น€์˜ฅ๋นˆ    #################################1(33%)
์ข‹์•„ํ•˜๋Š” ์—ฌ๋ฐฐ์šฐ?   ์•„์ด์œ     ###################################################################2(67%)
์ข‹์•„ํ•˜๋Š” ์—ฌ๋ฐฐ์šฐ?   ๊น€์„ ์•„    0(0%)




PL/SQL = Procedural Language extensions to SQL
                            ์ ˆ์ฐจ์  ์–ธ์–ด               ํ™•์žฅ๋œ             SQL
- IF
- ๋ณ€์ˆ˜์„ ์–ธ ( ์ƒ์ˆ˜, ๋ณ€์ˆ˜ ์‚ฌ์šฉ )
- FOR ๋ฌธ ( ํ๋ฆ„ ์ œ์–ด )
- ์˜ˆ์™ธ์ฒ˜๋ฆฌ ( ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ )
- procedure / function  ์ •์˜


1) PL/SQL์€ ๋ธ”๋Ÿญ ๊ตฌ์กฐ์˜ ์–ธ์–ด
- [์„œ๋ธŒ ๋ธ”๋Ÿญ]์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋…ผ๋ฆฌ์  ๋ธ”๋Ÿญ
- ์„ ์–ธ๋œ ๋ธ”๋Ÿญ ๋‚ด์—์„œ๋งŒ ์œ ํšจ

2) PL/SQL์€ 3๊ฐ€์ง€์˜ ๋ธ”๋Ÿญ ๊ตฌ์กฐ
[์„ ์–ธ ๋ธ”๋Ÿญ] - ๋ณ€์ˆ˜, ์ƒ์ˆ˜ ์„ ์–ธ
[์‹คํ–‰ ๋ธ”๋Ÿญ] - DML, DDL, TCL   SQL ์‹คํ–‰
[์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ธ”๋Ÿญ] - ์ƒ๋žต ๊ฐ€๋Šฅ

ํ˜•์‹)

 DECLARE 
   -- ์„ ์–ธ๋ธ”๋Ÿญ
 BEGIN
   -- ์‹คํ–‰ ๋ธ”๋Ÿญ 
   -- 1)
    INSERT INTO t_voter (VectorSeq         , UserName,RegDate, PollSeq,PollSubSeq, MemberSeq) 
    VALUES             ( seq_vecter.nextval, '๊ด€๋ฆฌ์ž', SYSDATE,  1,     2             , 1 );
    -- 2)
    UPDATE t_poll
    SET polltotal = polltotal +1
    WHERE pollseq = 1;
    --3)
    UPDATE t_pollsub
    SET acount = acount + 1
    WHERE pollsubseq = 2;
   
   COMMIT;
 EXCEPTION 
     -- ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ธ”๋Ÿญ
     ROLLBACK;
 END;

PL/SQL : 6๊ฐ€์ง€ ์ข…๋ฅ˜

1) anonymous procedure  ( ์ต๋ช… ํ”„๋กœ์‹œ์ € )
   DECLARE ...๋กœ ์‹œ์ž‘๋˜๋ฉฐ
   ์ €์žฅ X


2) stored procedure  ( ์ €์žฅ ํ”„๋กœ์‹œ์ € ) *****
   CREATE PROCEDURE name ...~~
   ์ €์žฅ O


3)stored function ( ์ €์žฅ ํ•จ์ˆ˜ )
    [์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ]๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ [๋ฐ˜ํ™˜]ํ•จ. 


4)package ( ํŒจํ‚ค์ง€ ) dbms_ranodm ํŒจํ‚ค์ง€   .value() ํ•จ์ˆ˜
  ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” + ๊ด€๋ จ์žˆ๋Š” ์—ฌ๋Ÿฌ procedure, function๋“ค์„ 
  ํ•˜๋‚˜์˜ package๋ฌถ์–ด์„œ ๊ด€๋ฆฌ์— ํŽธ๋ฆฌํ† ๋ก ํ•จ 

5)trigger ( ํŠธ๋ฆฌ๊ฑฐ ) 
  ์–ด๋–ค ์ž‘์—…์ „, ๋˜๋Š” ์ž‘์—… ํ›„ trigger์— ์ •์˜ํ•œ ๋กœ์ง์„ ์‹คํ–‰์‹œํ‚ค๋Š” PL/SQL ๋ธ”๋Ÿญ์ž„
  
6) X



1) anonymous procedure  ( ์ต๋ช… ํ”„๋กœ์‹œ์ € )

-- SELECT ์ฟผ๋ฆฌ ์‹คํ–‰ํ•ด์„œ ์‚ฌ์›๋ช…,sal๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ  ์ถœ๋ ฅ

DECLARE
  -- ์„ ์–ธ๋ธ”๋Ÿญ( ๋ณ€์ˆ˜ ์„ ์–ธ )
  -- v๋ณ€์ˆ˜๋ช… ์ž๋ฃŒํ˜•(ํฌ๊ธฐ);
  -- ENAME             VARCHAR2(10) 
  -- SAL               NUMBER(7,2) 
  vename varchar2(10);
  vsal number(7,2);
BEGIN
  -- ์‹คํ–‰๋ธ”๋Ÿญ
  SELECT ename, sal INTO vename, vsal
  FROM emp  
  WHERE empno = 7369;

DBMS_OUTPUT.PUT_LINE(vename);
DBMS_OUTPUT.PUT_LINE(vsal);
--EXCEPTION
END;


  

DBMS_OUTPUT ํŒจํ‚ค์ง€
1) PL/SQL ๋‚ด์—์„œ ์ฒ˜๋ฆฌ๋œ ์–ด๋–ค ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉ์ž์˜ ํ™”๋ฉด์— ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
2) PUT ๋˜๋Š”  PUT_LINE            : ์ •์˜๋œ ๋ฌธ์ž๊ฐ’์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ์„ธ์„œ 
    NEW_LINE                            : GET_LINE์— ์˜ํ•ด ์ฝํžŒ ํ–‰์˜ ๋‹ค์Œ ๋ผ์ธ์„ ์ฝ์„ ๋•Œ ์‚ฌ์šฉ 
    GET_LINE ๋˜๋Š” GET_LINES : ํ˜„์žฌ ๋ผ์ธ์˜ ๋ฌธ์ž๊ฐ’์„ ์ฝ๋Š” ํ”„๋กœ์„ธ์„œ 

    ENABLE      : ํ™”๋ฉด์— ๋ฌธ์ž๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜๋ฉฐ ๋ฌธ์ž๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํผํฌ๊ธฐ๋ฅผ ์ •์˜ํ•จ 
    DISABLE     : ํ™”๋ฉด์— ๋ฌธ์ž๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ชจ๋“œ๋ฅผ ํ•ด์ œํ•จ 



*๊ฒฐ๊ณผ ํ™•์ธํ•˜๋Š” ๋ฒ• : ๋ณด๊ธฐ -> DBMS ์ถœ๋ ฅ

PL/SQL ๋ฌธ๋ฒ•
-- ์˜ˆ) ์ด๋ฆ„, ๋‚˜์ด ๋ณ€์ˆ˜ ์„ ์–ธ + ๊ฐ’์„ ํ• ๋‹น -> ์ถœ๋ ฅ

DECLARE
  vname varchar2(10);
  vage  number(3);
BEGIN
   -- = ์ž๋ฐ” ๋Œ€์ž…์—ฐ์‚ฐ์ž   ==
   -- := ์˜ค๋ผํด ๋Œ€์ž…์—ฐ์‚ฐ์ž
   -- vname := 'ํ™๊ธธ๋™๋‹˜'; -- varchar2(10 byte)  ํ•œ๊ธ€ํ•œ๋ฌธ์ž=3๋ฐ”์ดํŠธ
   vname := 'ํ™๊ธธ๋™';
   vage := 20;
   
   DBMS_OUTPUT.PUT_LINE( vname || ', ' || vage );
--EXCEPTION
END;


* ๋Œ€์ž…      :=   INTO ๊ตฌ๋ฌธ ์‚ฌ์šฉํ•œ๋‹ค. 

-- 30๋ฒˆ ๋ถ€์„œ์˜ ์ง€์—ญ๋ช…*(loc) ๊ฐ€์ ธ์™€์„œ   10๋ฒˆ ๋ถ€์„œ์˜ loc ๋กœ ์ˆ˜์ •

DESC dept;
LOC             VARCHAR2(13)

DECLARE 
   -- ์ž๋ฐ” ๋ณ€์ˆ˜ ์„ ์–ธ ํ•  ๋•Œ ์ดˆ๊ธฐํ™”  int age = 10;
   -- vloc VARCHAR2(13);
   vloc dept.loc%TYPE := 'SEOUL' ; -- %TYPE    ํƒ€์ž…ํ˜• ๋ณ€์ˆ˜
BEGIN
   SELECT loc  INTO vloc
   FROM dept
   WHERE deptno = 30;
   
   UPDATE dept
   SET loc = vloc
   WHERE deptno = 10;
   
--   COMMIT;
-- EXCEPTION
--   ROLLBACK;
END;



-- ๋ฌธ์ œ) 10๋ฒˆ ๋ถ€์„œ์› ์ค‘์— ๊ธ‰์—ฌ๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ต๋ช… ํ”„๋กœ์‹œ์ € ์ž‘์„ฑ.

DECLARE
    vmax_pay NUMBER;
    
    vEMPNO      NUMBER(4) ;   
    vENAME      VARCHAR2(10) ;
    vJOB        VARCHAR2(9);       
    vHIREDATE   DATE ;        
    vpay        NUMBER; 
    vdeptno     NUMBER(2);
BEGIN
  SELECT MAX( sal + NVL( comm, 0 )) max_pay INTO vmax_pay
  FROM emp
  WHERE deptno = 10;
  
  SELECT empno, ename, deptno, job, hiredate, sal+NVL(comm, 0) pay
      INTO vempno, vename,  vdeptno, vjob, vhiredate , vpay
  FROM emp
  WHERE deptno = 10 AND sal+NVL(comm, 0) = vmax_pay;
  
  DBMS_OUTPUT.PUT_LINE( vempno || ', ' || vename || ', ' || vdeptno || ', ' || vjob || ', ' || vhiredate || ', ' ||  vpay   );
  
--EXCEPTION
END;




-- TYPE

DECLARE
    vmax_pay NUMBER;
    -- %TYPE ํƒ€์ž…ํ˜• ๋ณ€์ˆ˜๋กœ ์„ ์–ธ
    vEMPNO      emp.empno%TYPE ;   
    vENAME      emp.ename%TYPE ;
    vJOB        emp.job%TYPE;       
    vHIREDATE   emp.hiredate%TYPE ;        
    vpay        emp.sal%TYPE; 
    vdeptno     emp.deptno%TYPE;
BEGIN
  SELECT MAX( sal + NVL( comm, 0 )) max_pay INTO vmax_pay
  FROM emp
  WHERE deptno = 10;
  
  SELECT empno, ename, deptno, job, hiredate, sal+NVL(comm, 0) pay
      INTO vempno, vename,  vdeptno, vjob, vhiredate , vpay
  FROM emp
  WHERE deptno = 10 AND sal+NVL(comm, 0) = vmax_pay;
  
  DBMS_OUTPUT.PUT_LINE( vempno || ', ' || vename || ', ' || vdeptno || ', ' || vjob || ', ' || vhiredate || ', ' ||  vpay   );
  
--EXCEPTION
END;



-- ROWTYPE

DECLARE
    vmax_pay NUMBER;
    --emp ํ…Œ์ด๋ธ”์˜ ํ•œ ํ–‰(row, record ) ๋ณ€์ˆ˜ ์„ ์–ธ
    vemprow emp%ROWTYPE; -- %ํ–‰ํƒ€์ž… ๋ณ€์ˆ˜    
    vpay NUMBER;
BEGIN
  SELECT MAX( sal + NVL( comm, 0 )) max_pay INTO vmax_pay
  FROM emp
  WHERE deptno = 10;
  
  SELECT empno, ename, deptno, job, hiredate, sal+NVL(comm, 0) pay
      INTO vemprow.empno, vemprow.ename,  vemprow.deptno, vemprow.job, vemprow.hiredate 
      --, vemprow.sa
      , vpay
  FROM emp
  WHERE deptno = 10 AND sal+NVL(comm, 0) = vmax_pay;
  
  DBMS_OUTPUT.PUT_LINE( vemprow.empno || ', ' || vemprow.ename || ', ' || vemprow.deptno 
  || ', ' || vemprow.job || ', ' || vemprow.hiredate  || ', ' ||  vpay   );
  
--EXCEPTION
END;




PL/SQL ์˜  SELECT ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ–‰์„ ๋ฐ˜ํ™˜ํ•  ๊ฒฝ์šฐ์—๋Š”, ๋ฐ˜๋“œ์‹œ [์ปค์„œ(cursor)]๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ๋œ๋‹ค. ( ๊ธฐ์–ต)

DECLARE  
  vempno emp.empno%type;
  vename emp.ename%type;
BEGIN
  -- emp ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์‚ฌ์›์˜ empno, ename ์กฐํšŒํ•ด์„œ ์ถœ๋ ฅ...
   SELECT empno, ename  INTO vempno, vename
   FROM emp;
   --WHERE empno = 7369;
   
   DBMS_OUTPUT.PUT_LINE( vempno  || ', ' || vename );
-- EXCEPTION
END;

 


PL/SQL ํ๋ฆ„ ์ œ์–ด( ์ œ์–ด๋ฌธ ) 
-- IF ๋ฌธ์ž

์˜ค๋ผํด PL/SQL

IF ( ์กฐ๊ฑด์‹ ) THEN
   --
ELSIF ( ์กฐ๊ฑด์‹ ) THEN
   --
ELSIF ( ์กฐ๊ฑด์‹ ) THEN
   --
ELSIF ( ์กฐ๊ฑด์‹ ) THEN
    --
ELSE
   --
END IF;


--- ๋ฌธ์ œ) ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ์„ ์–ธํ•ด์„œ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ง์ˆ˜/ํ™€์ˆ˜ ์ถœ๋ ฅ. (์ž…๋ ฅ๋ฐ›๊ธฐ :binNumber)

DECLARE
   -- int n ;
   vn NUMBER := 0;
   vresult varchar2(10) := '์ง์ˆ˜';
BEGIN
    -- vn := scanner.nextInt();
    vn :=   :bindNumber;
    
   -- IF ( vn%2 != 0 ) THEN 
   IF (   MOD( vn, 2) != 0  ) THEN 
      vresult := 'ํ™€์ˆ˜';
    END IF;
    
    DBMS_OUTPUT.PUT_LINE(  vresult  );
-- EXCEPTION
END;


-- ๋ฌธ์ œ) ๊ตญ์–ด์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ˆ˜,์šฐ,๋ฏธ,์–‘,๊ฐ€ ์ถœ๋ ฅํ•˜๋Š” ์ต๋ช…ํ”„๋กœ์‹œ์ €๋ฅผ ์ž‘์„ฑ.
-- ( IF๋ฌธ ์‚ฌ์šฉ )

DECLARE
  vkor NUMBER(3) := 0;
  vgrade VARCHAR2(3);
BEGIN
   vkor :=    :bindKor;
   
   IF vkor >= 90 THEN
      vgrade := '์ˆ˜';
   ELSIF vkor >= 80 THEN
      vgrade := '์šฐ';
   ELSIF vkor >= 70 THEN
      vgrade := '๋ฏธ';
   ELSIF vkor >= 60 THEN
      vgrade := '์–‘';
   ELSE
     vgrade := '๊ฐ€';
   END IF;
   
   DBMS_OUTPUT.PUT_LINE( vgrade );
--EXCEPTION
END;

 

DECLARE
  vkor NUMBER(3) := 0;
  vgrade VARCHAR2(3);
BEGIN
   vkor :=    :bindKor;
   
   CASE   TRUNC(   vkor / 10  )
      WHEN  10 THEN vgrade := '์ˆ˜';
      WHEN  9 THEN vgrade := '์ˆ˜';
      WHEN  8 THEN vgrade := '์šฐ';
      WHEN  7 THEN vgrade := '๋ฏธ';
      WHEN  6 THEN vgrade := '์–‘';
      ELSE vgrade := '๊ฐ€';
   END CASE; 
   
--   CASE 
--      WHEN TRUNC(   vkor / 10  ) IN ( 10, 9 ) THEN vgrade := '์ˆ˜';
--   END CASE ;
   
   DBMS_OUTPUT.PUT_LINE( vgrade );
--EXCEPTION
END;


   
PL/SQL  FOR/WHILE ๋ฌธ

FOR  ์นด์šดํŠธ๋ณ€์ˆ˜  IN [REVERSE]  ์‹œ์ž‘๊ฐ’.. ๋๊ฐ’
LOOP
   -- ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ฝ”๋”ฉ.
END LOOP;
WHILE  (์กฐ๊ฑด์‹)
LOOP
   -- ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ฝ”๋”ฉ.
END LOOP;
LOOP
    -- ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ฝ”๋”ฉ.
   EXIT WHEN ์กฐ๊ฑด -- ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐˆ ์กฐ๊ฑด
END LOOP;


์˜ˆ) 1~10 ํ•ฉ ์ถœ๋ ฅ;  ( 1+2+3+4+5+6+7+8+9+10=55 )

DECLARE
   vi  NUMBER ;
   vsum NUMBER := 0;
BEGIN
   --FOR  vi  IN REVERSE 1.. 10
   FOR  vi  IN  1.. 10
   LOOP
      -- sum += i;
      -- vsum += vi;    += ๋ณตํ•ฉ๋Œ€์ž…์—ฐ์‚ฐ์ž X
      IF vi = 10 THEN
        DBMS_OUTPUT.PUT( vi  );
      ELSE
         DBMS_OUTPUT.PUT( vi ||  '+' );
      END IF;      
      vsum :=  vsum + vi;
   END LOOP;
   
   DBMS_OUTPUT.PUT_LINE('=' || vsum );

--EXCEPTION
END;



์˜ˆ) WHILE ๋ฌธ ์‚ฌ์šฉ 1~10 ํ•ฉ ์ถœ๋ ฅ;  ( 1+2+3+4+5+6+7+8+9+10=55 )

DECLARE
   vi  NUMBER := 1;
   vsum NUMBER := 0;
BEGIN
   vi := 1;
   WHILE ( vi <= 10 )
   LOOP
      IF vi = 10 THEN
        DBMS_OUTPUT.PUT( vi  );
      ELSE
         DBMS_OUTPUT.PUT( vi ||  '+' );
      END IF;      
      vsum :=  vsum + vi;
      -- vi++;   ++ ์ฆ๊ฐ์—ฐ์‚ฐ์ž ์—†์–ด์š”.
      vi := vi + 1 ;
   END LOOP;  
   DBMS_OUTPUT.PUT_LINE('=' || vsum );

--EXCEPTION
END;



์˜ˆ) do~ WHILE ๋ฌธ ์‚ฌ์šฉ 1~10 ํ•ฉ ์ถœ๋ ฅ;  ( 1+2+3+4+5+6+7+8+9+10=55 )

DECLARE
   vi  NUMBER := 1;
   vsum NUMBER := 0;
BEGIN
   vi := 1;
    
   LOOP
      IF vi = 10 THEN
        DBMS_OUTPUT.PUT( vi  );
      ELSE
         DBMS_OUTPUT.PUT( vi ||  '+' );
      END IF;      
      vsum :=  vsum + vi;
      -- vi++;   ++ ์ฆ๊ฐ์—ฐ์‚ฐ์ž ์—†์–ด์š”.
      EXIT WHEN vi = 10;
      vi := vi + 1 ;
   END LOOP;  
   DBMS_OUTPUT.PUT_LINE('=' || vsum );

--EXCEPTION
END;


   
-- ๋ฌธ์ œ) ๊ตฌ๊ตฌ๋‹จ 2~9๋‹จ
--      1) for ๋ฌธ์‚ฌ์šฉ  ( 2๊ฐœ )
--      2) while ๋ฌธ์‚ฌ์šฉ ( 2๊ฐœ )

DECLARE
  vdan NUMBER(2):=2 ;
  vi NUMBER(2) := 1 ;
BEGIN
   WHILE (vdan <= 9)
   LOOP
      vi  := 1;  -- *****
      WHILE( vi <= 9)
      LOOP
          DBMS_OUTPUT.PUT( vdan || '*' || vi || '=' || RPAD( vdan*vi, 4, ' ' ) );
          vi := vi + 1; -- 10
      END LOOP;
      DBMS_OUTPUT.PUT_LINE('');
      
      vdan := vdan + 1;
   END LOOP; 
--EXCEPTION
END;
DECLARE
  -- vdan NUMBER(1) ;
  -- vi NUMBER(1) ;
BEGIN

   FOR vdan IN 2.. 9
   LOOP 
      FOR vi IN 1.. 9
      LOOP
        -- System.out.printf("format")
        DBMS_OUTPUT.PUT( vdan || '*' || vi || '=' || RPAD( vdan*vi, 4, ' ' ) );
      END LOOP;
      DBMS_OUTPUT.PUT_LINE('');
   END LOOP;

--EXCEPTION
END;


-- PL/SQL  
-- 3๊ฐ€์ง€ ๋ธ”๋Ÿญ ๊ตฌ์กฐ
-- 6๊ฐ€์ง€ 1) ์ต๋ช…ํ”„๋กœ์‹œ์ €. + ๋ฌธ๋ฒ•
:=
INTO ์ ˆ
-- ์ปค์„œ( cursor )


[ RECORDํ˜• ๋ณ€์ˆ˜] : 1) TYPE, 2) ROWTYPE, 3) ์‚ฌ์šฉ์ž ์ •์˜ TYPE ๊ตฌ์กฐ์ฒด
deptno, dname, empno, ename, pay ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ๊ตฌ์กฐ์ฒด (์ž๋ฃŒํ˜•) ์„ ์–ธ

DECLARE
   -- ํƒ€์ž…( ์ž๋ฃŒํ˜• ) ์„ ์–ธ
   TYPE EmpDeptTYPE IS RECORD(
     deptno  dept.deptno%type,
     dname dept.dname%type,
     empno emp.empno%type,
     ename emp.ename%type,
     pay number
   );
   
   vedrow EmpDeptTYPE;  -- ๋ ˆ์ฝ”๋“œํ˜• ๋ณ€์ˆ˜ ์„ ์–ธ
BEGIN
    SELECT d.deptno, dname, empno, ename, sal + NVL(comm, 0) pay
         INTo vedrow.deptno, vedrow.dname, vedrow.empno, vedrow.ename  , vedrow.pay
    FROM dept d JOIN emp e  ON d.deptno = e.deptno
    WHERE empno = 7369;
    
    DBMS_OUTPUT.PUT_LINE( vedrow.deptno || ', ' || vedrow.dname  
    || ', ' ||  vedrow.empno  || ', ' || vedrow.ename  || ', ' ||  vedrow.pay );
--EXCEPTION
END;


์ปค์„œ( CURSOR ) 
1) ์ปค์„œ ? PL/SQL ๋ธ”๋Ÿญ ๋‚ด์—์„œ ์‹คํ–‰๋˜๋Š” [SELECT ๋ฌธ]์„ ์˜๋ฏธํ•œ๋‹ค.
2) SELECT ํ•œ ๊ฒฐ๊ณผ๊ฐ€ 1๊ฐœ X
          ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ–‰(๋ ˆ์ฝ”๋“œ)  -> ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ -> ์ปค์„œ
3) ์ปค์„œ 2๊ฐ€์ง€ ์ข…๋ฅ˜
   <1> ๋ฌต์‹œ์  ์ปค์„œ ( implicit ) == ์ž๋™ ์ปค์„œ

--DECLARE
  -- FOR ๋ฌธ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ˜๋ณต๋ณ€์ˆ˜( ์นด์šดํŠธ๋ณ€์ˆ˜) ๋Š” ์„ ์–ธํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค. 
  -- vrow dept%ROWTYPE;
BEGIN
-- EXCEPTION
   FOR  vrow IN (  SELECT  *    FROM dept  ) --  ์ปค์„œ ์ƒ์„ฑ
   LOOP
     DBMS_OUTPUT.PUT_LINE( vrow.deptno || ', ' || vrow.dname || ', ' || vrow.loc );
   END LOOP; 
END;

 

   
   <2> ๋ช…์‹œ์  ์ปค์„œ ( explicit )
      ใ„ฑ. ๋ช…์‹œ์  ์ปค์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ˆœ์„œ(๋‹จ๊ณ„)

         (1) ์ปค์„œ ์„ ์–ธ
         (2) OPEN -  SELECT๋ฌธ์„ ์‹ค์ œ ์‹คํ–‰ -> ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ(ํ–‰)์„ ์ปค์„œ ์ €์žฅ
         (3) FETCH
                    LOOP
                       
                       EXIT WHEN ???;
                    END LOOP;
          (4) CLOSE

    ใ„ด. ์ปค์„œ ์†์„ฑ

        (1) %ROWCOUNT : ์‹คํ–‰๋œ ์ปค์„œ๋ฌธ์žฅ์—์„œ ์ฝํžŒ ํ–‰์˜ ์ˆ˜ 
        (2) %FOUND          : ์‹คํ–‰๋œ ์ปค์„œ๋ฌธ์žฅ์—์„œ ๊ฒ€์ƒ‰๋œ ํ–‰์ด ๋ฐœ๊ฒฌ๋˜์—ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š” ์†์„ฑ 
        (3) %NOTFOUND  : ์‹คํ–‰๋œ ์ปค์„œ๋ฌธ์žฅ์—์„œ ๊ฒ€์ƒ‰๋œ ํ–‰์ด ๋ฐœ๊ฒฌ๋˜์ง€ ์•Š์•˜์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋Š” ์†์„ฑ 
        (4) %ISOPEN          : ์„ ์–ธ๋œ ์ปค์„œ๊ฐ€ ํ˜„์žฌ OPEN๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ๋ฐ˜ํ™˜ 

     ใ„ท.๋ช…์‹œ์  ์ปค์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ

DECLARE
   -- vrow emp%ROWTYPE;  ์ปฌ๋Ÿผ์ˆ˜๊ฐ€ 10๊ฐœ
   vempno emp.empno%type;
   vename emp.ename%type;
   vhiredate emp.hiredate%type;
   
   --(1) ์ปค์„œ ์„ ์–ธ
   -- CURSOR ์ปค์„œ๋ช… IS ( ์„œ๋ธŒ์ฟผ๋ฆฌ );
   CURSOR empcursor IS (
                        SELECT empno, ename, hiredate
                       FROM emp
                      );
BEGIN
   -- (2) ์ปค์„œ OPEN
   --  OPEN ์ปค์„œ๋ช…;
   OPEN empcursor;
   
   -- (3) FETCH == ๊ฐ€์ ธ์˜ค๋‹ค
   LOOP
       -- PLS-00394: wrong number of values in the INTO list of a FETCH statement
       --   FETCH ๋ฌธ์˜  INTO  ๋ชฉ๋ก์— ์ž˜๋ชป๋œ ์ˆ˜์˜ ๊ฐ’์žˆ๋‹ค.
       -- FETCH empcursor INTO vrow;
       FETCH empcursor INTO vempno, vename, vhiredate;
       DBMS_OUTPUT.PUT_LINE( vempno || ', ' || vename || ', ' || vhiredate ); 
      EXIT WHEN empcursor%NOTFOUND;
   END LOOP;
   
   CLOSE empcursor;
-- EXCEPTION
END;

* ๋งˆ์ง€๋ง‰์— ์ปค์„œ ๋‹ซ์•„์•ผํ•จ


2) STORED PROCEDURE( ์ €์žฅ ํ”„๋กœ์‹œ์ € ) -- ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ PL/SQL


- PL/SQL ์–ธ์–ด ์ค‘์—์„œ ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ๊ตฌ์กฐ
- ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž์ฃผ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ์—…๋ฌด ํ๋ฆ„์„ ์ด ๋ฌธ๋ฒ•์— ์˜ํ•ด ๋ฏธ๋ฆฌ ์ž‘์„ฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์— ์ €์žฅํ•ด ๋‘์—ˆ๋‹ค๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœํ•˜์—ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์‹คํ–‰ ์„ฑ๋Šฅ ๋นจ๋ผ์ง„๋‹ค.    
- ์„ ์–ธ ํ˜•์‹

CREATE [OR REPLACE] PROCEDURE ํ”„๋กœ์‹œ์ €๋ช…
(
   pํŒŒ๋ผ๋ฏธํ„ฐ๋ช… MODE(์ž…๋ ฅ/์ถœ๋ ฅ/์ž…์ถœ๋ ฅ์šฉ IN ,OUT , IN OUT)  ์ž๋ฃŒํ˜•(ํฌ๊ธฐ X),
   pํŒŒ๋ผ๋ฏธํ„ฐ๋ช… IN ์ž๋ฃŒํ˜•(ํฌ๊ธฐ X),
   pํŒŒ๋ผ๋ฏธํ„ฐ๋ช… ์ž๋ฃŒํ˜•(ํฌ๊ธฐ X)
)
IS
  -- v๋ณ€์ˆ˜๋ช… ์ž๋ฃŒํ˜•(ํฌ๊ธฐ)[:=์ดˆ๊ธฐ๊ฐ’];
  -- v๋ณ€์ˆ˜ ์„ ์–ธ;
BEGIN
   -- ์‹คํ–‰ ๋ธ”๋Ÿญ
EXCEPTION
   -- ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
END;


  
์˜ˆ) ์‚ญ์ œํ•  ๋ถ€์„œ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ์‚ญ์ œํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ € ์„ ์–ธ + ์‹คํ–‰

--  up_== user procedure
CREATE OR REPLACE PROCEDURE up_delDept
(
   pdeptno IN NUMBER
)
IS
  -- ์„ ์–ธ X
BEGIN
   DELETE FROM dept
   WHERE deptno = pdeptno;
   --COMMIT;
-- EXCEPTION
END up_delDept;

์ €์žฅ ํ”„๋กœ์‹œ์ €๋ฅผ ์‹คํ–‰ํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•
  1) execute ๋ฌธ   

  EXECUTE UP_DELDEPT(60);
  COMMIT;
  ROLLBACK;
  SELECT * FROM dept;


  2) ๋˜ ๋‹ค๋ฅธ ์ €์žฅ ํ”„๋กœ์‹œ์ € ์•ˆ์—์„œ ์‹คํ–‰..

CREATE OR REPLACE PROCEDURE up_exectest
IS
BEGIN
   UP_DELDEPT( 50 );
-- EXCEPTION
END;
EXEC up_exectest;


  3) ์ต๋ช… ํ”„๋กœ์‹œ์ € ์•ˆ์—์„œ ์‹คํ–‰

  DECLARE
  BEGIN
    UP_DELDEPT(50);
  --EXCEPTION
  END;

1) DEPT ํ…Œ์ด๋ธ”์— ์ƒˆ๋กœ์šด ๋ถ€์„œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ €

 CREATE SEQUENCE seq_dept
 INCREMENT BY 10
 START WITH 50
 MAXVALUE 90
 NOCACHE;


[DESC dept;]

DEPTNO NOT NULL NUMBER(2)    
DNAME           VARCHAR2(14)   ๋„ํ—ˆ์šฉ
LOC             VARCHAR2(13)   ๋„ํ—ˆ์šฉ

 

 CREATE OR REPLACE PROCEDURE up_insDept
 ( 
     pdname  dept.dname%type := null
     , ploc  dept.loc%type  DEFAULT null
 )
 IS
 BEGIN
    INSERT INTO dept ( deptno, dname, loc ) 
    VALUES ( seq_dept.nextval, pdname, ploc);
    COMMIT;
 --EXCEPTION
   -- ROLLBACK;
 END;
 EXEC UP_INSDEPT;
 EXEC UP_INSDEPT( pdname=>'QC');
 EXEC UP_INSDEPT( ploc =>'SEOUL');
 EXEC UP_INSDEPT( ploc =>'SEOUL' , pdname = 'QC');
 EXEC UP_INSDEPT(   'QC', 'SEOUL'  );



2) ๋ถ€์„œ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ €

CREATE OR REPLACE PROCEDURE up_udpDept
 ( 
       pdeptno  dept.deptno%type
     , pdname  dept.dname%type := null
     , ploc  dept.loc%type  DEFAULT null
 )
 IS
    vdname dept.dname%type;
    vloc dept.loc%type;
 BEGIN    
    
    IF  pdname  IS NULL OR ploc IS NULL THEN       
        --์›๋ž˜ ์ˆ˜์ • ์ „์˜ ๋ถ€์„œ๋ช…, ์ง€์—ญ๋ช… ์ €์žฅ
        SELECT dname, loc INTO  vdname, vloc
        FROM dept
        WHERE deptno =  pdeptno;
    END IF;
    UPDATE dept
    SET dname = CASE 
                   WHEN pdname IS NULL THEN vdname
                   ELSE pdname
               END
        , loc =     NVL( ploc, vloc )  
    WHERE deptno = pdeptno;
    COMMIT;
 --EXCEPTION
   -- ROLLBACK;
 END;
-- Procedure UP_UDPDEPT์ด(๊ฐ€) ์ปดํŒŒ์ผ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
EXEC up_udpDept( 60, 'QC','SEOUL');   -- ๋ถ€์„œ๋ช…, ์ง€์—ญ๋ช… ์ˆ˜์ •
EXEC up_udpDept( 60,  pdname => 'X'); -- ๋ถ€์„œ๋ช… ์ˆ˜์ •
EXEC up_udpDept( 60,  ploc => 'Y'); -- ์ง€์—ญ๋ช… ์ˆ˜์ •


3)              ๋ถ€์„œ๋ฅผ ์‚ญ์ œ ์ €์žฅํ”„๋กœ์‹œ์ €  : UP_DELDEPT( 60 ) X
4)              ๋ถ€์„œ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ €

CREATE OR REPLACE PROCEDURE up_selDept
--()
IS
   CURSOR vcur IS ( SELECT * FROM dept );
   vrow dept%ROWTYPE;
BEGIN  
  OPEN vcur;  
  LOOP
    FETCH vcur INTO vrow;
    EXIT WHEN vcur%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE( vrow.deptno || ', ' || vrow.dname || ', ' || vrow.loc );    
  END LOOP;  
  CLOSE vcur;
--EXCEPTIOn
END;
-- Procedure UP_SELDEPT์ด(๊ฐ€) ์ปดํŒŒ์ผ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


-- ๋ฌต์‹œ์  ์ปค์„œ ์‚ฌ์šฉ 

CREATE OR REPLACE PROCEDURE up_selDept 
IS
BEGIN  
  FOR vrow  IN ( SELECT * FROM dept)
  LOOP    
    DBMS_OUTPUT.PUT_LINE( vrow.deptno || ', ' || vrow.dname || ', ' || vrow.loc );    
  END LOOP;  
   
--EXCEPTIOn
END;
-- Procedure UP_SELDEPT์ด(๊ฐ€) ์ปดํŒŒ์ผ๋˜์—ˆ์Šต๋‹ˆ๋‹ค
EXEC UP_SELDEPT;


-- (  pdeptno IN ์ž…๋ ฅ์šฉ ํŒŒ๋ผ๋ฏธํ„ฐ )
-- (  OUT  ์ถœ๋ ฅ์šฉ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ €์žฅ ํ”„๋กœ์‹œ์ €์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ 1๊ฐœ )

์˜ˆ)  ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ ์ž…๋ ฅ์šฉ ๋งค๊ฐœ๋ณ€์ˆ˜ -> ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ ์•ž์ž๋ฆฌ 6๊ฐœ๋ฅผ ์ถœ๋ ฅ์šฉ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ฒ˜๋ฆฌ
ํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ €.

CREATE OR REPLACE PROCEDURE up_rr6
(
    pnum IN insa.num%type
    , prrn6 OUT VARCHAR2 -- ํฌ๊ธฐ X
)
IS
   vrrn insa.ssn%type;
BEGIN
   SELECT ssn INTO vrrn
   FROM insa
   WHERE num = pnum;
   
   prrn6 := SUBSTR( vrrn , 0, 6 );
-- EXCEPTION
END;
DECLARE
   vrrn6 VARCHAR2(6);
BEGIN
   UP_RR6( 1001, vrrn6);
   
   DBMS_OUTPUT.PUT_LINE( vrrn6 );
--EXCEPTION
END;

PL/SQL ์ €์žฅ ํ•จ์ˆ˜ ( Stored Function )

์ €์žฅ ํ”„๋กœ์‹œ์ € : ๋ฆฌํ„ด๊ฐ’์ด ์—†๋‹ค

์ €์žฅ ํ•จ์ˆ˜        : ๋ฆฌํ„ด๊ฐ’์ด ์žˆ๋‹ค

CREATE OR REPLACE FUNCTION uf_ํ•จ์ˆ˜๋ช…
()
RETURN ๋ฆฌํ„ด์ž๋ฃŒํ˜•
IS
BEGIN
  RETURN (๋ฆฌํ„ด๊ฐ’);
  RETURN ๋ฆฌํ„ด๊ฐ’;
EXCEPTION
END;



์˜ˆ) ์ €์žฅ ํ•จ์ˆ˜ : ์ฃผ๋ฏผ๋“ฑ๋ก ๋ฒˆํ˜ธ->> ์„ฑ๋ณ„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜   uf_gender

CREATE OR REPLACE FUNCTION uf_gender
(
   prrn VARCHAR2
)
RETURN VARCHAR2 -- ๋‚จ์ž, ์—ฌ์ž
IS
   vgender VARCHAR2(6);
BEGIN   
   IF  MOD( SUBSTR( prrn, -7, 1), 2) = 0  THEN  -- ์—ฌ์ž
      vgender := '์—ฌ์ž';
   ELSE  -- ๋‚จ์ž
     vgender := '๋‚จ์ž';
   END IF;   
   RETURN  vgender;
--EXCEPTION
END;
SELECT num, LENGTH(  name ), ssn, UF_GENDER( ssn ) gender
FROM insa;



-- ์˜ˆ)   1~ n ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ 

CREATE OR REPLACE FUNCTION uf_sum
(
   pn NUMBER
)
RETURN NUMBER
IS
   vsum  NUMBER := 0;
BEGIN   
   
   FOR  i IN 1..pn
   LOOP
      vsum := vsum + i;
   END LOOP;
     
   RETURN  vsum;
--EXCEPTION
END;
SELECT uf_sum(10), uf_sum( 100)
FROM dual;



-- ๋ณต์Šต ๋ฌธ์ œ --

๋ฌธ์ œ 1)tbl_score ํ…Œ์ด๋ธ”์— ์ƒˆ๋กœ์šด ํ•™์ƒ์˜ ์„ฑ์  ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ํ”„๋กœ์‹œ์ € : up_insScore
    p :   num, name,  kor, eng, mat   ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด 0 ์ฒ˜๋ฆฌ
    ์ด์ , ํ‰๊ท , ๋“ฑ๊ธ‰, ๋“ฑ์ˆ˜ ๊นŒ์ง€๋Š” ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๋„๋ก .. 
๋ฌธ์ œ 2) tbl_score ํ…Œ์ด๋ธ”์— ์ƒˆ๋กœ์šด ํ•™์ƒ์˜ ์„ฑ์  ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ํ”„๋กœ์‹œ์ € : up_updScore     
   p  : num,   kor, eng, mat  ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด  ์ˆ˜์ • ์ „์˜ ์ ์ˆ˜๋กœ ์ฒ˜๋ฆฌ
     ์ด์ , ํ‰๊ท , ๋“ฑ๊ธ‰ ๊นŒ์ง€๋Š” ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๋„๋ก .. 
๋ฌธ์ œ 3)      tbl_score ํ…Œ์ด๋ธ”์—  ์‚ญ์ œํ•˜๋Š” ํ”„๋กœ์‹œ์ € : up_delScore
  p  : num
๋ฌธ์ œ 4)   tbl_score ํ…Œ์ด๋ธ”์—  ๋ชจ๋“  ํ•™์ƒ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋Š” ํ”„๋กœ์‹œ์ € : up_selScore
๋ฌธ์ œ 5) ๋“ฑ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ”„๋กœ์‹œ์ €  : up_rankScore






 

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ