【问题标题】:How to pass Array to SQL Stored Procedure如何将数组传递给 SQL 存储过程
【发布时间】:2015-08-25 16:15:26
【问题描述】:

我正在尝试将数组传递给 DB2 存储过程,但遇到了问题。

这里有几个sn-ps的代码:

create type intArrayType as integer array[];

CREATE OR REPLACE PROCEDURE
array_trial (IN integer_array INTARRAYTYPE)

BEGIN
  SELECT UNNEST(integer_array) FROM sysibm.sysdummy1;
END 

它可以编译,但是当我尝试调用时:

CALL array_trial(ARRAY[1,2,3]);

我收到 -104 错误。

当我尝试从 RPGLE 调用时,我无法编译,因为它不喜欢数组

有什么想法吗?

【问题讨论】:

    标签: sql arrays db2-400 rpgle


    【解决方案1】:

    在 from 子句中使用 UNNEST,因为它创建了一个 temporary table...

    CREATE OR REPLACE PROCEDURE
    array_trial (IN integer_array INTARRAYTYPE)
    
    BEGIN
      declare c1 cursor with return to client for 
         SELECT * FROM UNNEST(integer_array) as rs;
      open c1;
    END;
    

    不幸的是,ARRAY 构造函数目前相当有限。 documentation 明确表示只能在 SET 变量或赋值语句的右侧指定。因此尝试像这样直接使用它是行不通的。

    CALL array_trial(ARRAY[1,2,3]);
    

    它返回以下消息:

    SQL State: 428H2
    Vendor Code: -20441
    Message: [SQ20441] Array type not valid where specified. 
    Cause . . . . . :   An array type was used but is not allowed in the 
    specified context.  Array types can only be used: -- As an argument of an 
    SQL or JAVA procedure. -- For an SQL variable declared in an SQL procedure.
    -- In a CAST specification in an SQL procedure. 
    Recovery  . . . :   Remove the reference to the array type. Try the request again.
    

    你可以构建一个驱动存储过程:

    create or replace procedure mysp
    begin
      declare myarray intArrayType;
      set myarray = ARRAY[1,2,3];
      call array_trial(myarray);
    end;
    

    然后调用它

    call mysp;
    

    据我目前所知,可以直接从另一个 SQL 过程或 Java 调用带有数组参数的 SP...但不能从 RPGLE 调用。

    【讨论】:

      猜你喜欢
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      相关资源
      最近更新 更多