Comparing records

Records can be compared with the == comparison operator and the .* notation.

It is possible to compare records by using the .* notation and the == or != operators:
IF rec1.* == rec2.* THEN
   ...
END IF

All members will be compared individually.

Important:

If the record contains members of type ARRAY[n], DYNAMIC ARRAY, DICTIONARY or TEXT/BYTE, the comparison will always evaluate to FALSE, except when TEXT/BYTE members are NULL in both records. Consider testing each record member individually. The size of the TEXT/BYTE members can be compared with the LENGTH() function.

If the record contains several levels of sub-records, all sub-records will be processed recursively.

If two record members are NULL, the result of this member comparison is TRUE.

If two corresponding members do not contain the same value, or one of them is NULL, the records are considered as different.

Tip:

Record comparison can be used to implement optimistic locking for database updates. For more details, read the SQL programming guide.

The next code example shows how to compare two records defined from the same type:
TYPE t_cust RECORD
            id INTEGER,
            name VARCHAR(50),
            address RECORD
                num VARCHAR(5),
                street VARCHAR(100)
            END RECORD
       END RECORD

MAIN
    DEFINE r1, r2 t_cust

    LET r1.id = 999
    LET r1.name = "Mike Torme"
    LET r1.address.num = "2A"
    LET r1.address.street = "Sunset bld"

    LET r2 = r1
    DISPLAY "1: ", IIF( r1.* == r2.*, "Equals", "Differs" )

    LET r2.name = "Mike Torm"
    DISPLAY "2: ", IIF( r1.* == r2.*, "Equals", "Differs" )

    LET r2.name = NULL
    DISPLAY "3: ", IIF( r1.* == r2.*, "Equals", "Differs" )

END MAIN
Output:
1: Equals
2: Differs
3: Differs