sqldb2wcs

Update 2 tables together which used to get content


I have two update query , both are using same condition to get data. Once one query run the condition won't bale to return any record. The first query

UPDATE catentdesc
SET PUBLISHED = 0
WHERE CATENTRY_ID IN (
        SELECT CATENTRY.CATENTRY_ID
        FROM CATENTDESC,
            CATENTRY
        WHERE CATENTRY.catenttype_id = 'ProductBean'
            AND CATENTDESC.CATENTRY_ID = CATENTRY.CATENTRY_ID
            AND catentry.buyable = 1
            AND catentdesc.published = 1
            AND CATENTRY.CATENTRY_ID IN (
                SELECT CATENTRY_ID
                FROM CATGPENREL
                WHERE CATALOG_ID = 10001
                )
            AND catentry.markfordelete = 0 minus
        SELECT CATENTRY.CATENTRY_ID
        FROM CATENTDESC,
            CATENTRY
        WHERE CATENTRY.catenttype_id = 'ProductBean'
            AND CATENTDESC.CATENTRY_ID = CATENTRY.CATENTRY_ID
            AND catentry.buyable = 1
            AND catentdesc.published = 1
            AND CATENTRY.CATENTRY_ID IN (
                SELECT CATENTRY_ID
                FROM CATGPENREL
                WHERE CATALOG_ID = 10051
                )
            AND catentry.markfordelete = 0
        )

The second query

UPDATE catentry
SET CATENTRY.BUYABLE = 0
WHERE CATENTRY_ID IN (
        SELECT CATENTRY.CATENTRY_ID
        FROM CATENTDESC,
            CATENTRY
        WHERE CATENTRY.catenttype_id = 'ProductBean'
            AND CATENTDESC.CATENTRY_ID = CATENTRY.CATENTRY_ID
            AND catentry.buyable = 1
            AND catentdesc.published = 1
            AND CATENTRY.CATENTRY_ID IN (
                SELECT CATENTRY_ID
                FROM CATGPENREL
                WHERE CATALOG_ID = 10001
                )
            AND catentry.markfordelete = 0 minus
        SELECT CATENTRY.CATENTRY_ID
        FROM CATENTDESC,
            CATENTRY
        WHERE CATENTRY.catenttype_id = 'ProductBean'
            AND CATENTDESC.CATENTRY_ID = CATENTRY.CATENTRY_ID
            AND catentry.buyable = 1
            AND catentdesc.published = 1
            AND CATENTRY.CATENTRY_ID IN (
                SELECT CATENTRY_ID
                FROM CATGPENREL
                WHERE CATALOG_ID = 10051
                )
            AND catentry.markfordelete = 0
        )

The problem is I need to update catentry and catentdesc , but if one update another one I am not able update because the condition query won't return any result.

It is possible with a procedure , by storing the result and update the table. But I am looking for a simpler way, can any one please help me.

Is there any way update both table at a time in DB2 ?


Solution

  • Since you don't specify a platform, I'm going to assume DB2 for Linux/Unix/Windows. If this isn't correct, it may still work on other Platforms, but I'm not sure.

    You could declare a temporary table, and store off the results to use in your updates later.

    Additionally, I think I was able to simplify your criteria query some by using a LEFT JOIN. It looks like you are taking all the CATENTRIES that are from catalog 10001, and then removing the entries (with the same criteria) that also exist in 10051. If that is the case, then the LEFT JOIN below should do the same thing. I can't really test it (without sample data), so you might want to test it yourself and ensure the results. :)

    DECLARE GLOBAL TEMPORARY TABLE SESSION.CATENTRY AS
        SELECT C.CATENTRY_ID
        FROM CATENTRY   C
        JOIN CATENTDESC D
          ON D.CATENTRY_ID = C.CATENTRY_ID
        JOIN CATGPENREL G
          ON G.CATENTRY_ID = C.CATENTRY_ID
        LEFT JOIN CATGPENREL G2
          ON G2.CATENTRY_ID = C.CATENTRY_ID
         AND G2.CATALOG_ID = 10051
        WHERE C.catenttype_id = 'ProductBean'
          AND C.buyable       = 1
          AND C.markfordelete = 0 
          AND D.published     = 1
          AND G.CATALOG_ID    = 10001
          AND G2.CATENTRY_ID IS NULL
    ON COMMIT PRESERVE ROWS
    ;
    
    UPDATE catentdesc
    SET PUBLISHED = 0
    WHERE CATENTRY_ID IN (
    WHERE CATENTRY_ID IN (
        SELECT CATENTRY_ID
        FROM SESSION.CATENTRY
    )
    ;
    
    UPDATE catentry
    SET BUYABLE = 0
    WHERE CATENTRY_ID IN (
        SELECT CATENTRY_ID
        FROM SESSION.CATENTRY
    )
    ;
    
    DROP TABLE SESSION.CATENTRY
    ;