Pass Array as parameter in DB2 Stored Procedure

六眼飞鱼酱① 提交于 2019-12-23 01:51:38

问题


I am trying to create a stored procedure which takes an array as a parameter and in the WHILE loop iterates through this array and adds the chars into a table.

For example if I had an array of ['a','b','c'] I would want to pass this into my stored procedure and the characters 'a' , 'b' and 'c' to be placed into a table.

My SP creates successfully, but I am having issues when I try to call my procedure. Can anybody point me towards how to pass in an array? My procedure is as follows....

    DROP PROCEDURE DB.LWRH_DYNAMIC_SP@
create type stringArray as VARCHAR(100) array[100]@
CREATE PROCEDURE DB.LWRH_SP
(
    IN list stringArray
)
LANGUAGE SQL
BEGIN
    DECLARE i, MAX INTEGER;
    DECLARE c CHAR(1);
    SET i = 0;
    SET MAX = CARDINALITY(list);
    WHILE i <= MAX DO
    SET c = list[i];
    INSERT INTO schema.test ("SERVICE TYPE")values (c);
    END WHILE;

END@

CALL DB.LWRH_SP('')@ 

回答1:


Use the DB2 array constructor to call a stored procedure with array typed input parameters.

An example of creating an array of integers using the array constructor:

ARRAY[1, 2, 3]

For your stored procedure example:

create type stringArray as VARCHAR(100) array[100]@
CREATE PROCEDURE DB.LWRH_SP
(
    IN list stringArray
)
LANGUAGE SQL
BEGIN
    DECLARE i, MAX INTEGER;
    DECLARE c CHAR(1);
    SET i = 0;
    SET MAX = CARDINALITY(list);
    WHILE i <= MAX DO
    SET c = list[i];
    INSERT INTO schema.test ("SERVICE TYPE")values (c);
    END WHILE;
END@

You can call the above stored procedure with an array of VARCHAR using:

CALL DB.LWRH_SP(ARRAY['a', 'b', 'c'])@ 

NOTE: In my experience some SQL developer tools (eg. DBArtisan) might not work with the above syntax of stored procedure call using the array constructor, but it definitely works with the Linux command line db2 tool aka. DB2 UDB CLP.




回答2:


You need to define the data type before, and then create a variable to pass like parameter.

>>-CREATE--+------------+--PROCEDURE--procedure-name------------>
       '-OR REPLACE-'                              

>--+--------------------------------------------------------------------------------+-->
   '-(--+----------------------------------------------------------------------+--)-'   
        | .-,----------------------------------------------------------------. |        
        | V .-IN----.                                                        | |        
        '---+-------+--parameter-name--| data-type |--+--------------------+-+-'        
            +-OUT---+                                 '-| default-clause |-'            
            '-INOUT-'                                                                   

>--| option-list |--| SQL-procedure-body |---------------------><

data-type

|--+-| built-in-type |---------------+--------------------------|
   +-| anchored-variable-data-type |-+   
   +-array-type-name-----------------+   
   +-cursor-type-name----------------+   
   +-distinct-type-name--------------+   
   '-row-type-name-------------------'   

Here, you can see an example of a function that receives an array as parameter. Note the array type was defined before the function. The same is necesary for stored procedures.

--#SET TERMINATOR @
create or replace type my_array_type as varchar(64) array[int]@
create or replace function card (in my_array my_array_type)
  returns int
 begin
  declare card int;
  set card = cardinality(my_array);
  return card;
 end@

create or replace procedure test ()
 begin
  declare size int;
  declare my my_array_type;
  set my [1] = 'uno';
  set my [2] = 'dos';
  set my [3] = 'tres';
  set size = card(my);
  CALL DBMS_OUTPUT.PUT('Cardinality = ');
  CALL DBMS_OUTPUT.PUT_LINE(size);
 end@

SET SERVEROUTPUT ON@

call test ()@

Remember that an array is different to a string (CHAR). The arrays are an internal object in DB2, and they need to be defined as variable before use them. Strings can be created as you did: ''. However, they are two different things in DB2.



来源:https://stackoverflow.com/questions/27246620/pass-array-as-parameter-in-db2-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!