[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 |
์ต๊ทผ๋๊ธ