【问题标题】:Pass a list/array in DB2 stored procedure在 DB2 存储过程中传递一个列表/数组
【发布时间】:2019-05-17 14:45:49
【问题描述】:
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)

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

【问题讨论】:

  • 什么平台和版本的 Db2?
  • 平台是windows和DB2 11
  • "Array" 表示某种编程语言;那么,您使用什么编程语言来调用该过程?

标签: list stored-procedures db2


【解决方案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;

【讨论】:

  • 在 db2 上运行上述内容时出现此错误——但这适用于 db fiddle。这些条款是互斥的。 SQLCODE=-628,SQLSTATE=42613,DRIVER=4.17.36 SQL 代码:-628,SQL 状态:42613
  • @Ryan 什么不完全有效? CREATE FUNCTION?它适用于我在 Linux 上的 11.1。它也必须在 Windows 上运行。
  • 感谢您的帮助,答案是正确的 - 它在 DB Fiddle 上运行良好,但在 DB2 Windows 上却不适合我。所以我接受了答案。现在我得到一个名为 tokenize 的 XQUERY 函数,在静态上下文中未定义 2 个参数。错误 QNAME=err:XPST0017。 SQLCODE=-16009, SQLSTATE=10506
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
相关资源
最近更新 更多