如何将数组传递给SQL存储过程
问题描述:
我试图将数组传递给DB2存储过程,并且遇到了问题。如何将数组传递给SQL存储过程
这里有一些代码片段:
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打电话,我不能编译,因为它不喜欢的阵列
任何想法?
答
UNNEST是在FROM子句中,因为它创建了一个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;
不幸的是使用数组构造器是目前相当有限。 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;
从我能够找到迄今为止的SP数组parm可以直接从另一个SQL过程或Java ...调用,但不能用RPGLE调用。