create object types, nested tables or VARRAY collections, and then use TABLE() in a SELECT statement
-- Object type representing an employee
CREATE OR REPLACE TYPE emp_obj AS OBJECT (
emp_id NUMBER,
emp_name VARCHAR2(50)
);
/
-- Nested table of emp_obj
CREATE OR REPLACE TYPE emp_tab AS TABLE OF emp_obj;
/
-- Table storing departments with employees as a collection
CREATE TABLE dept_with_emps (
dept_id NUMBER,
dept_name VARCHAR2(50),
employees emp_tab
) NESTED TABLE employees STORE AS employees_nt;
/
INSERT INTO dept_with_emps VALUES (
10,
'HR',
emp_tab(
emp_obj(101, 'Alice'),
emp_obj(102, 'Bob')
)
);
INSERT INTO dept_with_emps VALUES (
20,
'IT',
emp_tab(
emp_obj(201, 'Charlie'),
emp_obj(202, 'David'),
emp_obj(203, 'Eve')
)
);
COMMIT;
/
-- Select all employees from the nested collection
SELECT d.dept_id,
d.dept_name,
e.emp_id,
e.emp_name
FROM dept_with_emps d,
TABLE(d.employees) e;
/
No comments:
Post a Comment