Skip to content

Instantly share code, notes, and snippets.

@bouassaba
Last active March 29, 2019 23:22
Show Gist options
  • Save bouassaba/f1670d0da2308e941366b546b5c377f6 to your computer and use it in GitHub Desktop.
Save bouassaba/f1670d0da2308e941366b546b5c377f6 to your computer and use it in GitHub Desktop.
Oracle function return select query
CREATE TABLE product (
col1 VARCHAR2(255),
col2 NUMBER,
col3 VARCHAR2(255),
col4 NUMBER
);
INSERT INTO product (col1, col2, col3, col4)
VALUES ('A', 1, 'AA', 10);
INSERT INTO product (col1, col2, col3, col4)
VALUES ('C', 2, 'CC', 20);
INSERT INTO product (col1, col2, col3, col4)
VALUES ('B', 3, 'BB', 30);
CREATE TABLE employee (
col1 VARCHAR2(255),
col2 NUMBER,
col3 VARCHAR2(255),
col4 NUMBER
);
INSERT INTO employee (col1, col2, col3, col4)
VALUES ('X', 10, 'XX', 1);
INSERT INTO employee (col1, col2, col3, col4)
VALUES ('Y', 20, 'YY', 2);
INSERT INTO employee (col1, col2, col3, col4)
VALUES ('Z', 30, 'ZZ', 3);
CREATE OR REPLACE TYPE T_RECORD AS OBJECT (
col1 VARCHAR2(255),
col2 NUMBER,
col3 VARCHAR2(255),
col4 NUMBER
);
CREATE OR REPLACE TYPE T_TABLE AS TABLE OF T_RECORD;
CREATE OR REPLACE FUNCTION return_table RETURN T_TABLE AS
v_ret T_TABLE;
BEGIN
SELECT T_RECORD(e.col1, e.col2 * 100, e.col3, e.col4 * 100) BULK COLLECT INTO
v_ret
FROM employee e;
RETURN v_ret;
END return_table;
SELECT * FROM TABLE (return_table());
INSERT INTO product (col1, col2, col3, col4) (SELECT * FROM TABLE (return_table()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment