在DB2存储过程中传递列表/数组

时间:2019-05-17 14:45:49

标签: list stored-procedures db2

SELECT cc.clientid
FROM customer_client cc
GROUP BY cc.clientid
HAVING SUM(CASE WHEN cc.customerid IN (4567, 5678) THEN 1 ELSE 0 END) = COUNT(*) 
   AND COUNT(*) = 2;

我在Db2存储过程中调用此查询,在该过程中我必须传递客户ID列表-有任何可行的建议吗?

我尝试按照以下步骤通过它

CREATE PROCEDURE Find_Client_Customers (
    IN IN_CUSTIDS VARCHAR(1000),
    IN IN_CUST_COUNT INT)

但这会将列表作为字符串传递。

1 个答案:

答案 0 :(得分:1)

您可以使用string tokenizer

create function regexp_tokenize_number(
  source varchar(1024)
, pattern varchar(128))
returns table (seq int, tok bigint)
contains sql
deterministic
no external action
return
select seq, tok
from xmltable('for $id in tokenize($s, $p) return <i>{string($id)}</i>' 
passing 
  source as "s"
, pattern as "p"
columns 
  seq for ordinality
, tok bigint path 'if (. castable as xs:long) then xs:long(.) else ()'
) t;

select *
from table(regexp_tokenize_number('123, 456', ',')) t;

SEQ TOK
--- ---
  1 123
  2 456

在您的情况下:

SELECT cc.clientid
FROM customer_client cc
GROUP BY cc.clientid
HAVING SUM(CASE WHEN cc.customerid IN 
(
select t.tok
from table(regexp_tokenize_number('4567, 5678', ',')) t
) THEN 1 ELSE 0 END) = COUNT(*) 
AND COUNT(*) = 2;