Search code examples
db2cursorcobolmainframe

Mainframe COBOL db2 delete program


Detail question is my next question -- please check below link

DB2/Cursor program working in cobol


Solution

  • This should be a 2 step process. First identify the records that are to be deleted and then delete those records in the 2nd step. I think, you'll Not be able to delete the records from the same table you are fetching, that's why 2 steps.

    Step 1:

    Select Concat(A.ClientId, A.PhoneNumber, A.Timestamp) from MyTable A
     Where Concat(A.ClientId, A.PhoneNumber, A.Timestamp)
    Not IN (Select Concat(B.ClientId, B.PhoneNumber, Min(B.Timestamp))
        from MyTable B
        Group by B.ClientId, B.PhoneNumber);
    

    Step 2:

    Delete from MyTable
     Where Concat(A.ClientId, A.PhoneNumber, A.Timestamp) IN (all the values you got from Step1);
    

    You can run step 1, create a dataset to get all the values and use that dataset in step 2. If you are running dynamic then a cut paste will do, else you'll have to modify the dataset using SORT to create a query out of it. That will be another Step in the middle of step 1 and 2.

    .