简体   繁体   中英

How to give a nested table as a default parameter?

I have a function that take a nested table and integer as parameter.

I can create any default argument for the integer paramater. But for the nested table, I can give only an empty table.

function  a return packageName.tOfmyrecord(i in integer:=2,j in integer:=2, k in tableofmyrecord2 package.n)  PIPELINED
is 
begin
    --do its work
end

PLS-00653: aggregate/table functions are not allowed

I want to give as a default parameter the result of the function b which return a nested table. This solution works. But it's cumbersome. How can I do better?

function  a return packageName.tOfmyrecord  PIPELINED
is
        c packageName.myrecord;
        m_cur SYS_REFCURSOR;
begin
        OPEN m_cur FOR select * from table(  
             packageName.a(2,2,  packageName.b)); --I give the 3 default parameters. 2,2 for the integer and the table returns by the funciton b
        loop
            fetch m_cur into c;
            exit when m_cur%notfound;
            PIPE ROW(c);    
        end loop;
        close  m_cur;
 end;

I've tried this solution too but it does'nt work. Maybe there is only a little bit to change so that it works-

function  a return packageName.a  PIPELINED
is
begin
      return packagename(2,2,packagename.b);
end;

PLS_00633: return statement in a pipelined function cannot contain an expression.

Create the collection type in an SQL scope:

CREATE TYPE stringlist IS TABLE OF VARCHAR2(20);

Then you can use it in SQL statements and in PL/SQL (whereas, if you define the collection in a PL/SQL scope then you can only use it in PL/SQL).

Then you can define your package specification:

CREATE PACKAGE test_package IS
  FUNCTION test_fn(
    a IN INTEGER    DEFAULT 1,
    b IN stringlist DEFAULT stringlist('a','b','c')
  ) RETURN stringlist PIPELINED;
END;
/

And package body:

CREATE PACKAGE BODY test_package IS
  FUNCTION test_fn(
    a IN INTEGER    DEFAULT 1,
    b IN stringlist DEFAULT stringlist('a','b','c')
  ) RETURN stringlist PIPELINED
  IS
  BEGIN
    IF b IS NULL THEN
      RETURN;
    END IF;

    FOR i IN 1 .. b.COUNT LOOP
      PIPE ROW ( b(i) || a );
    END LOOP;
  END;
END;
/

And use it in SQL with the default arguments:

SELECT *
FROM   TABLE( test_package.test_fn() );

Which outputs:

\n|  COLUMN_VALUE | \n|  :----------- | \n|  a1 | \n|  b1 | \n|  c1 | \n

Or provide your own arguments:

SELECT *
FROM   TABLE(
         test_package.test_fn(
           b => stringlist('d','e','f','g')
         )
       );

Which outputs:

\n|  COLUMN_VALUE | \n|  :----------- | \n|  d1 | \n|  e1 | \n|  f1 | \n|  g1 | \n

db<>fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM