레코드(record)는 자료형이 각기 다른 데이터를 하나의 변수에 저장하는 데 사용한다.
TYPE 레코드 이름 IS RECORD(
변수 이름 자료형 NOT NULL := (또는 DEFAULT) 값 또는 값이 도출되는 여러 표현식
)
DECLARE
TYPE REC_DEPT IS RECORD(
deptno NUMBER(2) NOT NULL := 99,
dname DEPT.DNAME%TYPE,
loc DEPT.LOC%TYPE
);
dept_rec REC_DEPT;
BEGIN
dept_rec.deptno := 99;
dept_rec.dname := 'DATABASE';
dept_rec.loc := 'SEOUL';
DBMS_OUTPUT.PUT_LINE('DEPTNO : ' || dept_rec.deptno);
DBMS_OUTPUT.PUT_LINE('DNAME : ' || dept_rec.dname);
DBMS_OUTPUT.PUT_LINE('LOC : ' || dept_rec.loc);
END;
/
CREATE TABLE DEPT_RECORD
AS SELECT * FROM DEPT;
SELECT * FROM DEPT_RECORD;
DECLARE
TYPE REC_DEPT IS RECORD(
deptno NUMBER(2) NOT NULL := 99,
dname DEPT.DNAME%TYPE,
loc DEPT.LOC%TYPE
);
dept_rec REC_DEPT;
BEGIN
dept_rec.deptno := 99;
dept_rec.dname := 'DATABASE';
dept_rec.loc := 'SEOUL';
INSERT INTO DEPT_RECORD
VALUES dept_rec;
END;
/
SELECT * FROM DEPT_RECORD;
DECLARE
TYPE REC_DEPT IS RECORD(
deptno NUMBER(2) NOT NULL := 99,
dname DEPT.DNAME%TYPE,
loc DEPT.LOC%TYPE
);
dept_rec REC_DEPT;
BEGIN
dept_rec.deptno := 50;
dept_rec.dname := 'DB';
dept_rec.loc := 'SEOUL';
UPDATE DEPT_RECORD
SET ROW = dept_rec
WHERE DEPTNO = 99;
END;
/
SELECT * FROM DEPT_RECORD;
DECLARE
TYPE REC_DEPT IS RECORD(
deptno DEPT.DEPTNO%TYPE,
dname DEPT.DNAME%TYPE,
loc DEPT.LOC%TYPE
);
TYPE REC_EMP IS RECORD(
empno EMP.EMPNO%TYPE,
ename EMP.ENAME%TYPE,
dinfo REC_DEPT
);
emp_rec REC_EMP;
BEGIN
SELECT E.EMPNO, E.ENAME, D.DEPTNO, D.DNAME, D.LOC
INTO emp_rec.empno, emp_rec.ename,
emp_rec.dinfo.deptno,
emp_rec.dinfo.dname,
emp_rec.dinfo.loc
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO
AND E.EMPNO = 7788;
DBMS_OUTPUT.PUT_LINE('EMPNO : ' || emp_rec.empno);
DBMS_OUTPUT.PUT_LINE('ENAME : ' || emp_rec.ename);
DBMS_OUTPUT.PUT_LINE('DEPTNO : ' || emp_rec.dinfo.deptno);
DBMS_OUTPUT.PUT_LINE('DNAME : ' || emp_rec.dinfo.dname);
DBMS_OUTPUT.PUT_LINE('LOC : ' || emp_rec.dinfo.loc);
END;
/
TYPE 연관 배열 이름 IS TABLE OF 자료형 [NOT NULL] // 단일 자료형, 참조 자료형 사용 기능
INDEX BY 인덱스형; // 키로 사용할 인덱스의 자료형 지정, 정수나 문자 자료형 사용 가능
DECLARE
TYPE ITAB_EX IS TABLE OF VARCHAR2(20)
INDEX BY PLS_INTEGER;
text_arr ITAB_EX;
BEGIN
text_arr(1) := '1st data';
text_arr(2) := '2nd data';
text_arr(3) := '3rd data';
text_arr(4) := '4th data';
DBMS_OUTPUT.PUT_LINE('text_arr(1) : ' || text_arr(1));
DBMS_OUTPUT.PUT_LINE('text_arr(2) : ' || text_arr(2));
DBMS_OUTPUT.PUT_LINE('text_arr(3) : ' || text_arr(3));
DBMS_OUTPUT.PUT_LINE('text_arr(4) : ' || text_arr(4));
END;
/
DECLARE
TYPE REC_DEPT IS RECORD(
deptno DEPT.DEPTNO%TYPE,
dname DEPT.DNAME%TYPE
);
TYPE ITAB_DEPT IS TABLE OF REC_DEPT
INDEX BY PLS_INTEGER;
dept_arr ITAB_DEPT;
idx PLS_INTEGER := 0;
BEGIN
FOR i IN (SELECT DEPTNO, DNAME FROM DEPT) LOOP
idx := idx + 1;
dept_arr(idx).deptno := i.DEPTNO;
dept_arr(idx).dname := i.DNAME;
DBMS_OUTPUT.PUT_LINE(
dept_arr(idx).deptno || ' : ' || dept_arr(idx).dname);
END LOOP;
END;
/
DECLARE
TYPE ITAB_DEPT IS TABLE OF DEPT%ROWTYPE
INDEX BY PLS_INTEGER;
dept_arr ITAB_DEPT;
idx PLS_INTEGER := 0;
BEGIN
FOR i IN(SELECT * FROM DEPT) LOOP
idx := idx + 1;
dept_arr(idx).deptno := i.DEPTNO;
dept_arr(idx).dname := i.DNAME;
dept_arr(idx).loc := i.LOC;
DBMS_OUTPUT.PUT_LINE(
dept_arr(idx).deptno || ' : ' ||
dept_arr(idx).dname || ' : ' ||
dept_arr(idx).loc);
END LOOP;
END;
/
오라클에서는 컬렉션 사용상의 편의를 위해 몇 가지 서브프로그램을 제공하는데, 이를 컬렉션 메서드라고 한다.
컬렉션 메서드는 컬렉션과 관련된 다양한 정보 조회 기능을 제공한다.
컬렉션 내의 데이터 삭제나 컬렉션 크기 조절을 위한 특정 조작도 가능하다.
메서드 |
---|
EXISTS(n) |
COUNT |
LIMIT |
FIRST |
LAST |
PRIOR(n) |
NEXT(n) |
DELETE |
EXTEND |
TRIM |
컬렉션 메서드 사용하기
DECLARE
TYPE ITAB_EX IS TABLE OF VARCHAR2(20)
INDEX BY PLS_INTEGER;
text_arr ITAB_EX;
BEGIN
text_arr(1) := '1st data';
text_arr(2) := '2nd data';
text_arr(3) := '3rd data';
text_arr(50) := '50th data';
DBMS_OUTPUT.PUT_LINE('text_arr.COUNT : ' || text_arr.COUNT);
DBMS_OUTPUT.PUT_LINE('text_arr.FIRST : ' || text_arr.FIRST);
DBMS_OUTPUT.PUT_LINE('text_arr.LAST : ' || text_arr.LAST);
DBMS_OUTPUT.PUT_LINE('text_arr.PRIOR(50) : ' || text_arr.PRIOR(50));
DBMS_OUTPUT.PUT_LINE('text_arr.NEXT(50) : ' || text_arr.NEXT(50));
END;
/
Q1.
--①
CREATE TABLE EMP_RECORD
AS SELECT *
FROM EMP
WHERE 1<>1;
--②
DECLARE
TYPE REC_EMP IS RECORD (
empno EMP.EMPNO%TYPE NOT NULL := 9999,
ename EMP.ENAME%TYPE,
job EMP.JOB%TYPE,
mgr EMP.MGR%TYPE,
hiredate EMP.HIREDATE%TYPE,
sal EMP.SAL%TYPE,
comm EMP.COMM%TYPE,
deptno EMP.DEPTNO%TYPE
);
emp_rec REC_EMP;
BEGIN
emp_rec.empno := 1111;
emp_rec.ename := 'TEST_USER';
emp_rec.job := 'TEST_JOB';
emp_rec.mgr := null;
emp_rec.hiredate := TO_DATE('20180301','YYYYMMDD');
emp_rec.sal := 3000;
emp_rec.comm := null;
emp_rec.deptno := 40;
INSERT INTO EMP_RECORD
VALUES emp_rec;
END;
/
Q2.
DECLARE
TYPE ITAB_EMP IS TABLE OF EMP%ROWTYPE
INDEX BY PLS_INTEGER;
emp_arr ITAB_EMP;
idx PLS_INTEGER := 0;
BEGIN
FOR i IN (SELECT * FROM EMP) LOOP
idx := idx + 1;
emp_arr(idx).empno := i.EMPNO;
emp_arr(idx).ename := i.ENAME;
emp_arr(idx).job := i.JOB;
emp_arr(idx).mgr := i.MGR;
emp_arr(idx).hiredate := i.HIREDATE;
emp_arr(idx).sal := i.SAL;
emp_arr(idx).comm := i.COMM;
emp_arr(idx).deptno := i.DEPTNO;
DBMS_OUTPUT.PUT_LINE(
emp_arr(idx).empno || ' : ' ||
emp_arr(idx).ename || ' : ' ||
emp_arr(idx).job || ' : ' ||
emp_arr(idx).mgr || ' : ' ||
emp_arr(idx).hiredate || ' : ' ||
emp_arr(idx).sal || ' : ' ||
emp_arr(idx).comm || ' : ' ||
emp_arr(idx).deptno);
END LOOP;
END;
/