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
;