Joined: 25 Apr 2003 Posts: 31 Topics: 6 Location: Pune
Posted: Mon Jan 05, 2004 9:25 am Post subject: How to find missing record through JCL?
Hi All,
There is a DB2 table EMPLOYEE, with the following fields
Sr No. 9(03) <---- Primary Key, Autogenerated, starting from 1 incremented by 1 everytime the new record is added
Emp Name X(30)
Emp Address X(50)
...
etc.
Here the requirement is as below:
Say the table has 10000 records with Sr No starting from 1 to 10000. If I delete the record in between, then how can I find out the missing record's Sr No. programmtically?
Theres one way of doing it through a COBOL program as below:
Download the table in a FLAT file and then process it sequentially, Since the Sr No is starting from 1 and will be upto 10000, we can increment it in steps of 1 and compare it with Sr No, Wherever the record is missing, it will be displayed as deleted from table.
The other way of doing it is
2) Through EZTRIEVE also.
3) Through actually processing table in the COBOL program using cursor.
Do we have any other way of doing it?? Probably through DFSORT / ICETOOL???
Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
Posted: Mon Jan 05, 2004 11:22 am Post subject:
Mayuresh,
There are many ways of doing it. you can find the missing number with a DB2 query itself.you can even find the missing number with sort product.
DB2 Query:
Code:
SELECT A.SR_NO + 1 AS DELETED_ROW
FROM TABLE A
WHERE NOT EXISTS ( SELECT SR_NO
FROM TABLE B
WHERE B.SR_NO = A.SR_NO + 1)
AND A.TBL_NO <> 999
;
Sort Code:
The following DFSORT/ICETOOL jcl will give you the desired results.If you have syncsort at your shop then change the Pgm name to synctool.A brief explanation of the job. UNload the db2 table with just the Sr_no. The first copy operator splits the file into 3 different temp files. Temp T1 is just a copy of the input as is and temp file T2 will be a file by adding 1 to the sr_no and the third temp file will have just record with sr_no '001'.
Now we concatenate all the 3 files together and select all the records which do not have any duplicates.And the next copy step will again split into 2 files one which will have the missing sr_no and other is just a dummy file to eliminate the unwanted records.
Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
Posted: Mon Jan 05, 2004 3:17 pm Post subject:
Mayuresh,
Just for kicks here is an easytrieve code and cobol code which will give the desired results.However I deliberately coded a bug in all the versions(easytrieve as well as cobol). can you find that?
01 WS-NO PIC 9(03) VALUE ZERO.
01 S-EOF-FILE PIC X(01) VALUE 'N'.
PROCEDURE DIVISION.
PERFORM 1000-INITIALIZATION
PERFORM 2000-MAIN-PROCESS UNTIL S-EOF-FILE = 'Y'
PERFORM 3000-WRAPUP
GOBACK
.
1000-INITIALIZATION.
************************************************************
* THIS PARAGRAPH OPENS INPUT FILE AND DOES THE PRIME READ *
************************************************************
OPEN INPUT SR-FILE
PERFORM 2100-READ-SR-FILE
.
2000-MAIN-PROCESS.
************************************************************
*THIS PARAGRAPH PERFORMS THE MAIN LOGIC *
************************************************************
ADD +1 TO WS-NO
IF SR-NO = WS-NO
CONTINUE
ELSE
DISPLAY ' THE DELETED SR NO IS : ' WS-NO
ADD +1 TO WS-NO
END-IF
PERFORM 2100-READ-SR-FILE
.
2100-READ-SR-FILE.
************************************************************
*THIS PARAGRAPH READS THE SR-FILE *
************************************************************
READ SR-FILE
AT END
MOVE 'Y' TO S-EOF-FILE
END-READ
.
3000-WRAPUP.
************************************************************
* THIS PARAGRAPH CLOSES THE INPUT FILE. *
************************************************************
PERFORM UNTIL S-EOF-CURSOR = 'Y'
ADD +1 TO WS-NO
IF WS-SR-NO = WS-NO
CONTINUE
ELSE
DISPLAY ' THE DELETED SR NO IS:' WS-NO
ADD +1 TO WS-NO
END-IF
PERFORM 2100-FETCH-CURSOR
END-PERFORM
PERFORM 2200-CLOSE-CURSOR
GOBACK
.
2000-OPEN-CURSOR.
******************************************************************
** THIS PARA OPENS THE CURSOR SERIAL . **
******************************************************************
EXEC SQL
OPEN SERIAL
END-EXEC
EVALUATE SQLCODE
WHEN +0
CONTINUE
WHEN OTHER
MOVE SQLCODE TO WS-FORMAT-SQLCODE
DISPLAY 'THE SQLCODE IS: WS-FORMAT-SQLCODE
PERFORM INHOUSE-ABEND-ROUTINE
END-EVALUATE
.
2100-FETCH-CURSOR.
******************************************************************
** THIS PARA FETCHES THE CURSOR SERIAL . **
******************************************************************
EXEC SQL
FETCH SERIAL
INTO
:EMPLOYEE.SR-NO
END-EXEC
EVALUATE SQLCODE
WHEN +0
MOVE SR-NO OF TEND TO WS-SR-NO
WHEN +100
MOVE 'Y' TO S-EOF-CURSOR
WHEN OTHER
MOVE SQLCODE TO WS-FORMAT-SQLCODE
DISPLAY 'THE SQLCODE IS: WS-FORMAT-SQLCODE
PERFORM INHOUSE-ABEND-ROUTINE
END-EVALUATE
.
2200-CLOSE-CURSOR.
******************************************************************
** THIS PARA CLOSES THE CURSOR SERIAL. **
******************************************************************
EXEC SQL
CLOSE SERIAL
END-EXEC
EVALUATE SQLCODE
WHEN +0
CONTINUE
WHEN OTHER
MOVE SQLCODE TO WS-FORMAT-SQLCODE
DISPLAY 'THE SQLCODE IS: WS-FORMAT-SQLCODE
PERFORM INHOUSE-ABEND-ROUTINE
END-EVALUATE
.
Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
Posted: Tue Jan 06, 2004 5:29 am Post subject:
mayuresh,
A couple of corrections. In your very first post you said that the Sr_no is defined is 9(03), but your example data you show the maximum value of the SR_NO as 10000 which I don't think will fit in a 9(03).
Your guess about the bug in my code is not true.
I am not really sure of your alternate solution. My first impression of the solution is that might not work . The reason being simple that the difference between 2 sums will always result in a single digit. what happens if there is more than 1 record which is deleted?
Take the following data
Code:
Sr_no
001
002
003
005
006
007
009
010
In the above data I am missing sr_no 4 and 8. Now try to apply your sum logic and can you retrieve the 2 missing Sr_no ?
Joined: 25 Apr 2003 Posts: 31 Topics: 6 Location: Pune
Posted: Tue Jan 06, 2004 7:53 am Post subject:
Hello Kolusu,
My mistake, it should have been 9(5).
Ya about the BUG also my first guess was wrong. While giving the answer I did not looked at the data you have supplied.
In the easytrieve code, you have deleted the 2 records with Sr No 6 & 11. So this will not cause any problems till the fields are matching but once the code has found the mismatched record at WS-NO = 6, it will display all the other records to the mismatched condition as we are incrementing the WS-NO counter twice.
Code:
WS-NO = WS-NO + 1 ---> once
IF SR-NO = WS-NO
CONTINUE
ELSE
DISPLAY ' THE DELETED SR_NO IS : ' WS-NO
WS-NO = WS-NO + 1 ---> twice
END-IF
Same case applies to COBOL code also.
Code:
ADD +1 TO WS-NO ---> once
IF SR-NO = WS-NO
CONTINUE
ELSE
DISPLAY ' THE DELETED SR NO IS : ' WS-NO
ADD +1 TO WS-NO --> twice
END-IF
Yes, the SUM logic will only work when there is ONLY one record deleted.
Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
Posted: Tue Jan 06, 2004 9:01 am Post subject:
Mayuresh,
Thanks for clarifying about the size of the column. Now your second guess is also not accurate.
You said
Quote:
In the easytrieve code, you have deleted the 2 records with Sr No 6 & 11. So this will not cause any problems till the fields are matching but once the code has found the mismatched record at WS-NO = 6, it will display all the other records to the mismatched condition as we are incrementing the WS-NO counter twice.
Not exactly true. If you run the code shown above it will indeed display only the record numbers 6 and 11.
The bug I coded is for the condition when there are more than one successive deleted record.
i.e take the following example data
Code:
001
002
007
008
009
010
Here I deleted srno's 3,4,5,6. It is in this case that you get the wrong results. To fix this you need to a perform loop in cobol and do loop in easytrieve
i.e
Easytrieve code
Code:
WS-NO = WS-NO + 1
IF SR-NO = WS-NO
CONTINUE
ELSE
DO UNTIL WS-NO >= SR-NO <=== new code start
DISPLAY ' THE DELETED SR_NO IS : ' WS-NO
WS-NO = WS-NO + 1
END-DO <=== new code end
END-IF
cobol code:
Code:
ADD +1 TO WS-NO
IF SR-NO = WS-NO
CONTINUE
ELSE
PERFORM UNTIL WS-NO >= SR-NO <==== new code start
DISPLAY ' THE DELETED SR NO IS : ' WS-NO
ADD +1 TO WS-NO
END-PERFORM <==== new code end
END-IF
Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
Posted: Wed Jan 14, 2004 2:28 pm Post subject:
coolman,
Yes ofcourse it does not find the missing record if the deleted record happens to be the very first record. hmm I need to fix it.
Here is another version of DFSORT/ICETOOL which will find all the missing seqnum.You need to have DFSORT PTF UQ90053 installed for this job to work. A brief explanation of the job. The first sort operator will sort the file on seqnum descending so that I will get the max seqnum in the table. It create control file for generating the seqnum upto the max seqnum.
The second copy operator will the control file created in the first sort step and generates seqnum file using REPEAT parm,so that the file will have seqnum in serial upto the max number.
Now concatenate both the files(input & generated senum file) and using SELECT with NODUPS to find the missing numbers.
Joined: 02 Dec 2002 Posts: 1618 Topics: 31 Location: San Jose
Posted: Thu Jan 15, 2004 10:44 am Post subject:
I just realized that we can change Kolusu's DFSORT/ICETOOL job slightly to replace the SORT with a COPY by using TRAILER1 to create the OUTREC statement. TRAILER1 will pick up the sequence number from the last record. Here's the modified DFSORT/ICETOOL job:
_________________ Frank Yaeger - DFSORT Development Team (IBM)
Specialties: JOINKEYS, FINDREP, WHEN=GROUP, ICETOOL, Symbols, Migration
DFSORT is on the Web at:
www.ibm.com/storage/dfsort
Joined: 02 Dec 2002 Posts: 1618 Topics: 31 Location: San Jose
Posted: Thu Jan 15, 2004 1:08 pm Post subject:
Kolusu,
Well, for that matter, why didn't I think of it when we were discussing this problem offline. _________________ Frank Yaeger - DFSORT Development Team (IBM)
Specialties: JOINKEYS, FINDREP, WHEN=GROUP, ICETOOL, Symbols, Migration
DFSORT is on the Web at:
www.ibm.com/storage/dfsort
Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
Posted: Thu Jan 15, 2004 3:01 pm Post subject:
Frank,
Aha I remember now why I wanted to sort the data instead of copy.I need the max but not the last record. The last record is not always the max seqnum. what if the data is not sorted on the seqnum?.
Joined: 02 Dec 2002 Posts: 1618 Topics: 31 Location: San Jose
Posted: Thu Jan 15, 2004 3:38 pm Post subject:
Kolusu,
The first post says
Quote:
Say the table has 10000 records with Sr No starting from 1 to 10000
and
Quote:
Since the Sr No is starting from 1 and will be upto 10000, we can increment it in steps of 1 and compare it with Sr No
I took that to mean that the sequence numbers are in order. Am I misinterpreting those statements? _________________ Frank Yaeger - DFSORT Development Team (IBM)
Specialties: JOINKEYS, FINDREP, WHEN=GROUP, ICETOOL, Symbols, Migration
DFSORT is on the Web at:
www.ibm.com/storage/dfsort
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum