如何在Oracle

时间:2018-08-12 23:47:07

标签: oracle plsql

我正在尝试执行具有多个值参数的函数。

SELECT * 
FROM TABLE(MyFunction('1,2')); 

这就是我创建函数的方式

create or replace FUNCTION MyFunction
(
  -- Add the parameters for the function here
  Ids varchar2
)

--
//Extra bit and pieces

如果我创建单值函数,

create or replace FUNCTION MyFunction
(
  -- Add the parameters for the function here
  p_party_id IN NUMBER
)

像这样执行

SELECT * 
FROM TABLE(MyFunction(1));

然后上述查询对我来说很好。

2 个答案:

答案 0 :(得分:1)

您创建的函数

create or replace FUNCTION MyFunction
  (
  -- Add the parameters for the function here
  Ids varchar2
  )

仍然只使用一个参数。如果要传递多个参数,则需要声明每个参数,如:

create or replace FUNCTION MyFunction
  (
  -- Add the parameters for the function here
  Id_1 varchar2,
  Id_2 varchar2
  )

好运。

答案 1 :(得分:1)

啊哈,所以您要传递以逗号分隔的值作为参数。如果是这样,则必须将其分割成行,并使用单独的值进行某些操作

下面是一个示例,该示例返回输入字符串作为行,然后与DEPT表(由Scott拥有)联接。您可能要对此做一些不同的事情。

SQL> create or replace function myfunction(par_ids in varchar2)
  2    return sys.odcinumberlist
  3  is
  4    t_id sys.odcinumberlist := sys.odcinumberlist();
  5  begin
  6    select regexp_substr(par_ids, '[^,]+', 1, level)
  7    bulk collect into t_id
  8    from dual
  9    connect by level <= regexp_count(par_ids, ',') + 1;
 10
 11    return t_id;
 12  end;
 13  /

Function created.

SQL> select x.column_value, d.dname, d.loc
  2  from dept d join table(myfunction('10,20,30')) x on x.column_value = d.deptno;

COLUMN_VALUE DNAME          LOC
------------ -------------- -------------
          10 ACCOUNTING     NEW YORK
          20 RESEARCH       DALLAS
          30 SALES          CHICAGO

SQL>