0
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;

I'm calling this query in a Db2 stored procedure where in I've to pass the list of customer id - any working suggestion?

I've tried passing it as below in procedure

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

but this is passing the list as a string.

3
  • what platform and version of Db2? Commented May 17, 2019 at 15:18
  • Platform is windows and DB2 11 Commented May 17, 2019 at 15:28
  • "Array" implies some programming language; so, what programming language are you using to call the procedure? Commented May 17, 2019 at 16:17

1 Answer 1

2

You may use a 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

In your case:

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Getting this error while running the above on db2 -- this works on db fiddle though. THE CLAUSES ARE MUTUALLY EXCLUSIVE. SQLCODE=-628, SQLSTATE=42613, DRIVER=4.17.36 SQL Code: -628, SQL State: 42613
@Ryan What doesn't work exactly? CREATE FUNCTION? It works on my 11.1 on Linux. It must work on Windows as well.
thanks for the help the answer is right - it runs fine on DB Fiddle but somehow not working for me on DB2 windows.. so I've accepted the answer. Now I'm getting AN XQUERY FUNCTION NAMED tokenize WITH 2 PARAMETERS IS NOT DEFINED IN THE STATIC CONTEXT. ERROR QNAME=err:XPST0017. SQLCODE=-16009, SQLSTATE=10506

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.