【问题标题】:How to call pl-sql function that takes table of numbers as input with 1 number?如何调用 pl-sql 函数,该函数将数字表作为 1 个数字的输入?
【发布时间】:2015-11-25 14:08:07
【问题描述】:

我有一个将数字表作为输入的函数:

function foo(ids in custom_array)...

其中custom_array 是一个数字表。

如果我只需要传递 1 个参数,如何从 SQL 查询中调用此函数?

我想这应该是这样的

select * from table(foo(123))

但我的猜测是错误的。

编辑: 函数的声明如下所示:

create or replace function foo(id in custom_array) return
tableof2numbers AS
    mytable tableof2numbers;
    BEGIN
      SELECT cast(
          MULTISET (

/*there goes some super secret business logic*/
/*let's say that here we just do smth like this: */

select 123, 123 from dual

/*because this is not so important for this question*/
          )
          AS tableof2numbers)
      INTO
        mytable
      FROM dual;
      RETURN mytable;

其中tableof2numbers 是另一种自定义类型,其声明与custom_array 非常相似,但有两个数字。

提前致谢!!!

【问题讨论】:

  • 我收到这个错误:ORA-06553: PLS-306: wrong number or types of arguments in call to 'foo'
  • 请显示 custom_array 类型和 foo 的完整定义。
  • foo 返回什么?您的示例调用似乎暗示它返回一个集合。您当然有可能拥有一个将集合作为输入并返回集合作为输出的函数,但也有可能您使用了错误的语法来调用它。参数可能应该是foo(custom_array(123)),但没有足够的信息来了解完整的select 语句。
  • @JustinCave 我收回我的评论“不,语法是正确的” - 我把 foo 误认为是数字类型表的名称。
  • select * from table(sys.odcinumberlist(1,2,3,4,5,6,7,8)) where column_value

标签: sql oracle function plsql


【解决方案1】:

我的例子:

1) 类型

create or replace TYPE "TABLE_OF_NUMBER"    
AS TABLE OF NUMBER

2) 功能

CREATE OR REPLACE FUNCTION foo(
    par IN table_of_number)
  RETURN NUMBER
IS
BEGIN
  RETURN par(1);
END;

3) 选择

SELECT foo(table_of_number(123)) FROM dual

如果您的函数返回表,它将类似于

SELECT * FROM TABLE (foo(custom_array(123)))

希望它会有所帮助。

【讨论】:

    猜你喜欢
    • 2020-06-25
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 2019-05-26
    • 2019-07-23
    相关资源
    最近更新 更多