User Tools

Site Tools


prop_duplicatekey.htm
Navigation:  Language Reference > App C - PROP: Runtime Properties > Runtime VIEW and FILE Properties >====== PROP:DuplicateKey ====== Previous pageReturn to chapter overviewNext page

Read-Only property of a FILE that returns the key that caused the last duplicate key error. It is cleared only when a subsequent ADD or PUT is attempted.

It is very common that you want to know which key causes a duplicate key error after you do a PUT or an ADD. At present you do this by looping through all keys that a file has and testing for DUPLICATE for each key until you find one. This is inefficient and requires a fair amount of code. Generally, the file drivers already know which key you are looking for, so why not ask the driver for the key. You can now do this using PROP:DuplicateKey.

Example:

 PROGRAM
 MAP
 END
f FILE,DRIVER('Clarion'),CREATE,PRE(f)
pk KEY(f:fld1)
  RECORD
fld1 LONG
  END
 END
k &KEY
 CODE
  CREATE(f)
  OPEN(f)
  f.fld1 = 1
  ADD(f)
  f.fld1 = 2
  ADD(f)
  k &= f{PROP:DuplicateKey} ! k is NULL at this stage because no duplicate has occurred
  f.fld1 = 1
  PUT(f)
  k &= f{PROP:DuplicateKey} ! k will be set to f:pk as the PUT caused a
                            ! duplicate key error
  SET(pk)
  GET(pk)
  f.fld1 = 7
  k &= f{PROP:DuplicateKey} ! k will be still be set at this stage
  PUT(f)
  k &= f{PROP:DuplicateKey} ! k will be NULL now
prop_duplicatekey.htm.txt · Last modified: 2021/04/15 15:57 (external edit)