Manage Learn to apply best practices and optimize your operations.

Comparing new records to existing records

I'm creating a new record and want to compare it with an existing record on file. If they are identical I drop the new one, otherwise I write the new one to the file. The file can have a duplicate key. Can it be done without comparing every field?
You can name a complete record from a database file by declaring an externally-defined data structure. Create two for the same file and use the PREFIX keyword on one of them:

D Rec1          E DS                  ExtName(WMETPCPF)                 
D Rec2          E DS                  ExtName(WMETPCPF) Prefix(D1:2) 

Now the fields in the file WMETPCPF are contained by name in structure REC1, and by a slightly different name in REC2. So you can refer to field TPVDRN (which is the field in REC1) or to field D1VDRN in structure REC2.

I use this to load screens from files. When you read a record from the file, its data will fill the fields in REC1. If you then do:

 C                   Eval      Rec2 = Rec1  

...both structures now contain the same data. The fields on the screen match the fields from the file.

This means that you can do your new record duplication detection in one compare operation. First, the fields containing your new data are in one structure, and the fields from the database record are in another. So, just compare REC1 with REC2 and you have compared ALL the fields.

BUT, you might not want to compare ALL the fields! Some fields, like timestamps, are not relevant. So, set those to a known value before comparing the structures:

C                   Eval      TPDATE = D1DATE 
C                   Eval      TPTIME = D1TIME 
C                   Eval      TPUSER = D1USER 

C                   If        Rec1 <> Rec2 
C                   Eval      Rec1  = Rec2 
C                   Write (E) TP 
C                   EndIf 


The Best Web Links: tips, tutorials and more.

Visit the ITKnowledge Exchange and get answers to your developing questions fast.

Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.

Dig Deeper on Data backup, storage and retrieval on iSeries

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.