Ask the Expert

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 

==================================
MORE INFORMATION ON THIS TOPIC
==================================

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.

This was first published in March 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: