package
CREATE OR REPLACE PACKAGE poly_pkg AS
FUNCTION describe (p_tbl IN OUT DBMS_TF.TABLE_T )
RETURN DBMS_TF.DESCRIBE_T;
PROCEDURE fetch_rows;
END;
Package created.
package body
CREATE OR REPLACE PACKAGE BODY poly_pkg AS
FUNCTION describe (p_tbl IN OUT DBMS_TF.TABLE_T )
RETURN DBMS_TF.DESCRIBE_T AS
-- metadata for column to add
v_new_col DBMS_TF.COLUMN_METADATA_T;
-- table of columns to add
v_new_cols DBMS_TF.COLUMNS_NEW_T;
BEGIN
-- define metadata for column named ANOTHER_COL1
-- that will default to a datatype of varchar2 with
-- a length of 4000
v_new_col := DBMS_TF.COLUMN_METADATA_T(
name => 'ANOTHER_COL1' );
-- add ANOTHER_COL1 to the list of columns new columns
v_new_cols := DBMS_TF.COLUMNS_NEW_T( 1 => v_new_col);
-- Instead of returning NULL we will RETURN a specific
-- DESCRIBE_T that adds new columns
RETURN DBMS_TF.DESCRIBE_T ( new_columns => v_new_cols );
END;
PROCEDURE fetch_rows IS
v_rowset DBMS_TF.ROW_SET_T;
v_rowcount PLS_INTEGER;
v_another_col DBMS_TF.TAB_VARCHAR2_T;
BEGIN
-- fetch rows into a local rowset
-- at this point the rows will have columns
-- from the the table/view/query passed in
DBMS_TF.GET_ROW_SET(v_rowset,v_rowcount);
-- for every row in the rowset...
FOR counter IN 1..v_rowcount LOOP
-- specify a value for the new column in
-- the row
v_another_col(counter) := 1;
END LOOP; -- every row in the rowset
-- add the newly populated column to the rowset
DBMS_TF.PUT_COL( columnid => 1,
collection => v_another_col);
END;
END;
Package Body created.
function
CREATE OR REPLACE FUNCTION poly_func(p_tbl IN TABLE)
RETURN TABLE PIPELINED
ROW POLYMORPHIC USING poly_pkg;
Function created.
demo table
CREATE TABLE table1
( col1 NUMBER,
col2 NUMBER )
Table created.
demo data
INSERT INTO table1
VALUES(19,99)
1 row(s) inserted.
demo view
CREATE VIEW who_knows AS
SELECT *
FROM poly_func(table1)
View created.
view columns
SELECT column_name,
data_type
FROM user_tab_cols
WHERE table_name = 'WHO_KNOWS'
COLUMN_NAME | DATA_TYPE | COL1 | NUMBER | COL2 | NUMBER | ANOTHER_COL1 | VARCHAR2 |
---|
view query
SELECT *
FROM who_knows
COL1 | COL2 | ANOTHER_COL1 | 19 | 99 | 1 |
---|