0

We have following query to get the records deleted on fk_data_table, and been found we weren’t able to, as because of 400k records.

Delete FROM BOM_LINK WHERE TEST_OBJECT_OID IN (SELECT DISTINCT TESTOBJECT_OID FROM TESTOBJECT WHERE TESTOBJECT.TESTOBJECTTYPE_OID = 3);

DB2 Error Code -495 https://www.sqlerror.de/db2_sql_error_-495_sqlstate_57051.html

I think what we can do is,

  1. We can write a function or procedure
  2. We can retrieve data by writing query by SELECT and CURSOR options, instead directly deleting
  3. While iterating CURSOR we can try deleting it. (I am not sure that in this way we can delete the row, anyway lets find this out)
  4. We can do periodical commit, instead of doing commit after extensive amount of deletion.

Could someone help us sorting out this issue which we are facing. Pointing to some SQL code snippets will help us a lot.

James Z
  • 12,209
  • 10
  • 24
  • 44
Dev Anand Sadasivam
  • 699
  • 7
  • 21
  • 49

1 Answers1

1

Unfortunately, Db2 for Z/OS doesn't allow to delete from a subselect.
I don't have Db2 for Z/OS at hand to check, but you may try the following:

CREATE VIEW BOM_LINK_V AS
SELECT *
FROM BOM_LINK B
WHERE EXISTS
(
  SELECT 1
  FROM TESTOBJECT T
  WHERE T.TESTOBJECT_OID = B.TEST_OBJECT_OID
  AND T.TESTOBJECTTYPE_OID = 3
)
FETCH FIRST 1000 ROWS ONLY;

Run DELETE FROM BOM_LINK_V until you get SQLSTATE = '02000' (no rows affected).

Update:
The DELETE statement since v12 supports the FETCH FIRST clause.
So, you may run the following without creating a view until you get SQLSTATE = '02000':

DELETE FROM BOM_LINK B
WHERE EXISTS
(
  SELECT 1
  FROM TESTOBJECT T
  WHERE T.TESTOBJECT_OID = B.TEST_OBJECT_OID
  AND T.TESTOBJECTTYPE_OID = 3
)
FETCH FIRST 1000 ROWS ONLY;
Mark Barinstein
  • 11,456
  • 2
  • 8
  • 16
  • The `DB2` is hosted on `Z OS` and we're working as a ***DB Client*** from `Windows`. And we found we are able to do the deletion for sub-select , I mean by using sub queries. – Dev Anand Sadasivam Aug 18 '22 at 09:09
  • The client type doesn't matter. The query runs by the server, and it doesn't support delete on subselect, according to the link above. – Mark Barinstein Aug 18 '22 at 09:28
  • Ok. I didn't check myself. But my colleague had shown that it works. I didn't probe the `SQL` execution result completely. As he'd said it is working, I had agreed. Anyway I took your consent on this matter. Thanks :) – Dev Anand Sadasivam Aug 18 '22 at 09:36