[오라클PL/SQL,NETSTED TABLE, TABLE TYPE]예제
SQL> CREATE TYPE emp_type AS OBJECT
(
empno NUMBER(4),
ename VARCHAR2(50)
);
/
유형이 생성되었습니다.
SQL> CREATE TYPE emp_type_table AS TABLE OF emp_type;
/
유형이 생성되었습니다.
SQL> desc emp_type
이름 널? 유형
----------------------------------------- -------- -------------
EMPNO NUMBER(4)
ENAME VARCHAR2(50)
SQL> desc emp_type_table
emp_type_table TABLE OF EMP_TYPE
이름 널? 유형
----------------------------------------- -------- -------------
EMPNO NUMBER(4)
ENAME VARCHAR2(50)
SQL> select text
from user_source
where name = 'EMP_TYPE';
TEXT
-----------------------------------------------
TYPE emp_type AS OBJECT
(
empno NUMBER(4),
ename VARCHAR2(50)
);
SQL> select text
2 from dba_source
3 where name = 'EMP_TYPE_TABLE';
TEXT
------------------------------------------------
TYPE emp_type_table AS TABLE OF emp_type;
SQL> CREATE TABLE dept_nt (
2 deptno NUMBER(4),
3 dname VARCHAR2(20),
4 emps EMP_TYPE_TABLE)
5 NESTED TABLE emps STORE AS emps_table;
테이블이 생성되었습니다.
SQL> desc dept_nt
이름 널? 유형
----------------------------------------- -------- --------------
DEPTNO NUMBER(4)
DNAME VARCHAR2(20)
EMPS EMP_TYPE_TABLE
SQL> desc emps_table
이름 널? 유형
----------------------------------------- -------- --------------
EMPNO NUMBER(4)
ENAME VARCHAR2(50)
SQL> SELECT table_name, column_name, data_type, data_length
FROM dba_tab_cols
WHERE table_name = 'DEPT_NT';
TABLE_NAME COLUMN_NAME DATA_TYPE DATA_LENGTH
---------- ------------------- ------------------- -----------
DEPT_NT EMPS EMP_TYPE_TABLE 16
DEPT_NT SYS_NC0000300004$ RAW 16
DEPT_NT DNAME VARCHAR2 20
DEPT_NT DEPTNO NUMBER 22
SQL> SELECT table_name, table_type_name,parent_table_column
FROM dba_nested_tables
WHERE table_name = 'EMPS_TABLE';
TABLE_NAME TABLE_TYPE_NAME PARENT_TABLE_COLUMN
---------------- -------------------------- -----------------------
EMPS_TABLE EMP_TYPE_TABLE EMPS
SQL> select table_name,column_name,data_type
from dba_nested_table_cols
where table_name = 'EMPS_TABLE';
TABLE_NAME COLUMN_NAME DATA_TYPE
-------------------- ------------------------------ ------------
EMPS_TABLE SYS_NC_ROWINFO$ EMP_TYPE
EMPS_TABLE ENAME VARCHAR2
EMPS_TABLE EMPNO NUMBER
EMPS_TABLE NESTED_TABLE_ID RAW
SQL> INSERT INTO dept_nt (deptno, dname, emps)
VALUES (10, '영업부',emp_type_table());
1 row created.
SQL> INSERT INTO TABLE(SELECT emps FROM dept_nt WHERE deptno = 10)
VALUES (1, '1길동');
SQL> INSERT INTO TABLE(SELECT emps FROM dept_nt WHERE deptno = 10)
VALUES (2, '2길동');
SQL> INSERT INTO TABLE(SELECT emps FROM dept_nt WHERE deptno = 10)
VALUES (3, '3길동');
SQL> select t1.deptno, t1.dname,t2.* from dept_nt t1, table(t1.emps) t2;
DEPTNO DNAME EMPNO ENAME
-------------------------------
10 영업부 1 1길동
10 영업부 2 2길동
10 영업부 3 3길동
댓글 없음:
댓글 쓰기