简体   繁体   English

如何将嵌套表作为默认参数?

[英]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 PLS-00653: 不允许聚合/表函数

I want to give as a default parameter the result of the function b which return a nested table.我想将返回嵌套表的函数 b 的结果作为默认参数。 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. PLS_00633:流水线函数中的 return 语句不能包含表达式。

Create the collection type in an SQL scope:在 SQL 范围内创建集合类型:

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).然后您可以在 SQL 语句和 PL/SQL 中使用它(而如果您在 PL/SQL 范围内定义集合,那么您只能在 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:并使用默认参数在 SQL 中使用它:

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

Which outputs:哪些输出:

\n| | COLUMN_VALUE | COLUMN_VALUE |\n| | :----------- | :----------- |\n| | a1 | a1 |\n| | b1 | b1 |\n| | c1 | 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 | COLUMN_VALUE |\n| | :----------- | :----------- |\n| | d1 | d1 |\n| | e1 | e1 |\n| | f1 | f1 |\n| | g1 | g1 |\n

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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