Search code examples
sqldb2zos

DB2 z/OS update by list


i have a list of 3k ID's in text file. I should find rows with such ID's and update them. I would expect to work something like

WITH RECORD_ID
(ID) AS (
'12345',
...
'32134')
UPDATE MY_TABLE T
SET T.NAME = REPLACE(T.NAME, X'03', '')
WHERE EXISTS (SELECT 1 FROM RECORD_ID R WHERE R.ID = T.ID);

But that doesn't work. Any idea?


Solution

  • You can't just have a list of values in a CTE in DB2, you have to UNION ALL the values together:

    WITH RECORD_ID (ID) AS (
        SELECT '12345' FROM SYSIBM.SYSDUMMY1 UNION ALL
        -- ...
        SELECT '32134' FROM SYSIBM.SYSDUMMY1
    )
    UPDATE MY_TABLE T
    SET T.NAME = REPLACE(T.NAME, X'03', '')
    WHERE EXISTS (SELECT 1 FROM RECORD_ID R WHERE R.ID = T.ID);
    

    Should work.

    I agree with @X-Zero, though... it seems that a list of values in an IN clause would be more appropriate, unless there's a specific reason you want to use the CTE.