Create the implementation package named skip_col_pkg containing the DESCRIBE function for the skip_col polymorphic table function (PTF).
CREATE PACKAGE skip_col_pkg AS
-- OVERLOAD 1: Skip by name --
FUNCTION skip_col(tab TABLE,
col columns)
RETURN TABLE PIPELINED ROW POLYMORPHIC USING skip_col_pkg;
FUNCTION describe(tab IN OUT dbms_tf.table_t,
col dbms_tf.columns_t)
RETURN dbms_tf.describe_t;
-- OVERLOAD 2: Skip by type --
FUNCTION skip_col(tab TABLE,
type_name VARCHAR2,
flip VARCHAR2 DEFAULT 'False')
RETURN TABLE PIPELINED ROW POLYMORPHIC USING skip_col_pkg;
FUNCTION describe(tab IN OUT dbms_tf.table_t,
type_name VARCHAR2,
flip VARCHAR2 DEFAULT 'False')
RETURN dbms_tf.describe_t;
END skip_col_pkg;
Package created.
Create the implementation package body which contains the polymorphic table function definition.
CREATE PACKAGE BODY skip_col_pkg AS
/* OVERLOAD 1: Skip by name
* NAME: skip_col_pkg.skip_col
* ALIAS: skip_col_by_name
*
* PARAMETERS:
* tab - The input table
* col - The name of the columns to drop from the output
*
* DESCRIPTION:
* This PTF removes all the input columns listed in col from the output
* of the PTF.
*/
FUNCTION describe(tab IN OUT dbms_tf.table_t,
col dbms_tf.columns_t)
RETURN dbms_tf.describe_t
AS
new_cols dbms_tf.columns_new_t;
col_id PLS_INTEGER := 1;
BEGIN
FOR i IN 1 .. tab.column.count() LOOP
FOR j IN 1 .. col.count() LOOP
tab.column(i).pass_through := tab.column(i).description.name != col(j);
EXIT WHEN NOT tab.column(i).pass_through;
END LOOP;
END LOOP;
RETURN NULL;
END;
/* OVERLOAD 2: Skip by type
* NAME: skip_col_pkg.skip_col
* ALIAS: skip_col_by_type
*
* PARAMETERS:
* tab - Input table
* type_name - A string representing the type of columns to skip
* flip - 'False' [default] => Match columns with given type_name
* otherwise => Ignore columns with given type_name
*
* DESCRIPTION:
* This PTF removes the given type of columns from the given table.
*/
FUNCTION describe(tab IN OUT dbms_tf.table_t,
type_name VARCHAR2,
flip VARCHAR2 DEFAULT 'False')
RETURN dbms_tf.describe_t
AS
typ CONSTANT VARCHAR2(1024) := upper(trim(type_name));
BEGIN
FOR i IN 1 .. tab.column.count() LOOP
tab.column(i).pass_through :=
CASE upper(substr(flip,1,1))
WHEN 'F' THEN dbms_tf.column_type_name(tab.column(i).description)!=typ
ELSE dbms_tf.column_type_name(tab.column(i).description) =typ
END /* case */;
END LOOP;
RETURN NULL;
END;
END skip_col_pkg;
Package Body created.
Create a standalone polymorphic table function named skip_col_by_name for overload 1.
CREATE FUNCTION skip_col_by_name(tab TABLE,
col columns)
RETURN TABLE PIPELINED ROW POLYMORPHIC USING skip_col_pkg;
Function created.
Create a standalone polymorphic table function named skip_col_by_type for overload 2.
CREATE FUNCTION skip_col_by_type(tab TABLE,
type_name VARCHAR2,
flip VARCHAR2 DEFAULT 'False')
RETURN TABLE PIPELINED ROW POLYMORPHIC USING skip_col_pkg;
Function created.
SELECT * FROM skip_col_pkg.skip_col(scott.dept, 'number')
DNAME | LOC |
---|---|
ACCOUNTING | NEW YORK |
RESEARCH | DALLAS |
SALES | CHICAGO |
OPERATIONS | BOSTON |
SELECT * FROM skip_col_by_type(scott.dept, 'number')
DNAME | LOC |
---|---|
ACCOUNTING | NEW YORK |
RESEARCH | DALLAS |
SALES | CHICAGO |
OPERATIONS | BOSTON |
SELECT * FROM skip_col_pkg.skip_col(scott.dept, 'number', flip => 'True')
DEPTNO |
---|
10 |
20 |
30 |
40 |
SELECT *
FROM skip_col_by_type(scott.dept, 'number', flip => 'True')
DEPTNO |
---|
10 |
20 |
30 |
40 |
SELECT *
FROM skip_col_by_name(scott.emp, columns(comm, hiredate, mgr))
WHERE deptno = 20
EMPNO | ENAME | JOB | SAL | DEPTNO |
---|---|---|---|---|
7566 | JONES | MANAGER | 2975 | 20 |
7788 | SCOTT | ANALYST | 3000 | 20 |
7902 | FORD | ANALYST | 3000 | 20 |
7369 | SMITH | CLERK | 800 | 20 |
7876 | ADAMS | CLERK | 1100 | 20 |