Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DUPLICATE KEY 1

Status
Not open for further replies.

claudeb

Programmer
Nov 23, 2000
140
CA
hi
how do i check for duplicate key before writing into a file ?
if the record is already there (according to the key), skip the writing.
thanks
 
Hi Claude,

Back with the small dogs, I see. Anyway, I assume you are trying to load a VSAM file, two things you can do:

1) If you know how to use DFSORT or SYNCSORT you should sort the ip file by the VSAM key first, using:

sum fields=none

This gets rid of any dups.

2) Instead of issuing a read statement in line, perform a
read routine:
Code:
    05  ws-valid-rec-sw      pic x(01) value 'r'.
        88  reset-valid-rec-sw         value 'r'.
        88  valid-rec-found            value 'f'.

    05  ws-prev-key          pic x(??) value space


01  ws-curr-rec.
    05  ws-curr-key         pic x(??) value space.
    05  rest of curr
    05              record 
    05                     description


      7000-get-valid-rec.
          set reset-valid-rec-sw    to true
          perform until valid-rec-found        
             move ws-curr-key       to ws-prev-key
             read ip-rec          into ws-curr-rec
               at end
                  set ip-eof        to true
                  move all x'ff'    to ws-curr-key
              not at end
                  add +1            to ip-cntr
             end-read
             if ws-curr-key not      = ws-prev-key
                set valid-rec-found to true
             else
                add +1              to dup-cntr
             end-if
          end-perform

This code only returns non-dup recs on a read. It takes the "tekkie" type logic out of the mainline and allows the reader to concentrate on the "business" logic.

Hope this helps. Standard disclaimers apply, Jack.

 
Hi Claude -
Slade's answer will work great, but this sounds more like a class assignment to me from the way you worded it, and if it is, your instructor won't like that solution. Accordingly, I offer a solution using a strict interpretation of what you asked for.

MOVE desired-key-value TO VSAM-key-variable.
READ VSAM-file
INVALID KEY
PERFORM WRITE-TO-VSAM-FILE-ROUTINE
NOT INVALID KEY
PEFORM DUPLICATE-KEY-ERROR-ROUTINE
END-READ.

Hope this helps. Betty Scherber
Brainbench MVP for COBOL II
 
Hi Betty,

I think you've got the invalid key processing reversed.

If Claude is trying to complete a class assignment, I agree that your solution will satisfy the prof. It should also be noted that in the so called real world, unproductive writes to a VSAM file are frowned upon. On the otherhand, if they are minimal its probably ok.

Please understand, I'm not trying to denigrate your solution; just stating a fact that might be helpful to those just starting out.

Regards, Jack.

 
Thanks. No big , no small dogs. just a program that loops and loops and loops...
 
The INVALID KEY logic on the READ is right (possibly you were thinking of a WRITE, which wouldn't solve the problem) -
If the READ didn't work (INVALID KEY), then there is no duplicate key in the file, so go ahead and write
If the READ did work (NOT INVALID KEY), the key already exists in the file, so do the error logic

Betty Scherber
Brainbench MVP for COBOL II
 
You're right, Betty; I thought it was a write. That leads to another question: would a write be a better in that situation? One i/o vs two.
 
You're right, Jack. A WRITE would be more efficient than a READ, but it didn't address the solution to the strictest interpretation of the question. Betty Scherber
Brainbench MVP for COBOL II
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top