Create the PTF implementation package rand_col_p.
CREATE PACKAGE rand_col_p AS
FUNCTION describe(tab IN OUT DBMS_TF.table_t,
col_count NATURALN DEFAULT 1,
low NUMBER DEFAULT NULL,
high NUMBER DEFAULT NULL)
RETURN DBMS_TF.describe_t;
PROCEDURE fetch_rows(col_count NATURALN DEFAULT 1,
low NUMBER DEFAULT NULL,
high NUMBER DEFAULT NULL);
END rand_col_p;
Package created.
Create the PTF implementation package body rand_col_p.
CREATE PACKAGE BODY rand_col_p AS
col_name_prefix CONSTANT dbms_id := 'RAND_';
FUNCTION describe(tab IN OUT DBMS_TF.table_t,
col_count NATURALN DEFAULT 1,
low NUMBER DEFAULT NULL,
high NUMBER DEFAULT NULL)
RETURN DBMS_TF.describe_t
AS
cols DBMS_TF.columns_new_t;
BEGIN
FOR i IN 1 .. col_count LOOP
cols(i):= DBMS_TF.column_metadata_t(name=>col_name_prefix||i, TYPE=>DBMS_TF.type_number);
END LOOP;
RETURN DBMS_TF.describe_t(new_columns => cols);
END;
PROCEDURE fetch_rows(col_count NATURALN DEFAULT 1,
low NUMBER DEFAULT NULL,
high NUMBER DEFAULT NULL)
AS
row_count CONSTANT PLS_INTEGER := DBMS_TF.get_env().row_count;
col DBMS_TF.tab_number_t;
BEGIN
FOR c IN 1 .. col_count LOOP
FOR i IN 1 .. row_count LOOP
col(i) := CASE WHEN (low IS NULL OR high IS NULL)
THEN dbms_random.VALUE
ELSE dbms_random.VALUE(low, high)
END;
END LOOP;
DBMS_TF.put_col(c, col);
END LOOP;
END;
END rand_col_p;
Package Body created.
Create the standalone rand_col PTF.
CREATE FUNCTION rand_col(tab TABLE,
col_count NATURALN DEFAULT 1,
low NUMBER DEFAULT NULL,
high NUMBER DEFAULT NULL)
RETURN TABLE
PIPELINED ROW POLYMORPHIC USING rand_col_p;
Function created.
SELECT * FROM rand_col(scott.dept)
DEPTNO | DNAME | LOC | RAND_1 | 10 | ACCOUNTING | NEW YORK | .55945637047454787427586751649477353358 | 20 | RESEARCH | DALLAS | .07098278777676773258461895904487782397 | 30 | SALES | CHICAGO | .53474236157131789196313476225626719444 | 40 | OPERATIONS | BOSTON | .12878037114181358966326499316272410643 |
---|
SELECT *
FROM rand_col(scott.dept, col_count => 2)
DEPTNO | DNAME | LOC | RAND_1 | RAND_2 | 10 | ACCOUNTING | NEW YORK | .57478647920519041450136378470339922172 | .66206037476603089635366074655940478022 | 20 | RESEARCH | DALLAS | .83947496883154142566877492915652489815 | .68151628839732411463090335064106019501 | 30 | SALES | CHICAGO | .86952742871799371774384501660978659682 | .57123198830715037456613696515295439343 | 40 | OPERATIONS | BOSTON | .58516150786062359490952907521759867605 | .22233979350121358649531083175094856545 |
---|
SELECT ename, job, rand_1, rand_2, rand_3
FROM rand_col(scott.emp, col_count => 3, low => -10, high => +10)
WHERE job != 'SALESMAN'
ENAME | JOB | RAND_1 | RAND_2 | RAND_3 | KING | PRESIDENT | -6.9719709939329613655796549402018300316 | 1.3183455948722047611448757415664313998 | 9.7241529579655902775499833379838104794 | BLAKE | MANAGER | 5.166747254239014292673841369563018813 | -4.9191526004082859983104737858757063976 | 8.589388987572087402617623989070550617 | CLARK | MANAGER | -.2711281577000331396799228398451532372 | -.2739950397121260019356736062931095406 | 1.326262670356115640754734659504627155 | JONES | MANAGER | 7.931683120884415291328959406273482017 | 8.3156157600736793092776422779535997822 | -7.0917688037567756293016461296047761098 | SCOTT | ANALYST | 9.761125101703031167631735441350208102 | 2.1278964168583414459209563326617729186 | -.7580510798588551804796670871521265716 | FORD | ANALYST | -2.9696460616201513457937756326182297494 | -5.0210559273065212398691573979670140592 | 5.6616354563809145489951236425894540398 | SMITH | CLERK | -4.8774855830367578403364674296084082574 | -6.0976608544445786806271149750320595922 | 8.9597494388914161200366797991155343138 | ADAMS | CLERK | -5.5952785025602039076700205478464693748 | 8.3407569902950397392933781059274475874 | 7.5262318339102002661325786708199530316 | JAMES | CLERK | -9.1703936543767469005713387431082204192 | 9.281281355679506037195421937349265687 | 4.4523031786714156979252793805843259022 | MILLER | CLERK | -6.9398146083716569809969616767418185958 | -.508877372084361584498851751309568578 | -8.5732029130279494156221708869678517466 |
---|