Search code examples
cobolmicrofocus

how to update primary key of index file in microfocus cobol


I need to update the primary key of a indexed record which I am reading it sequentially i know that i cannot use rewrite to do that, Any suggestion

The first 5 move statement is primary key.

           move perdt1-pno         to perat-pno
           move zeros              to perat-bno
           move space              to perat-eng-type
           move space              to perat-shift-code
           move zeros              to perat-area
           start perat-file key > perat-key
           invalid key
               set end-of-infile         to true
           not invalid key

             
               read perat-file next ignore lock
               at end
                   continue
               end-read

               
               display "meow"
               perform 
               varying ws-sub from 1 by 1
               until ws-sub > 10
               or perat-pno not = perdt1-pno
               
               
                   if  (perat-pno = perdt1-pno)
                   and (in-apc <> perat-bno)
                       
                       display "meow2"
                       display perat-pno
                       move in-apc     to perat1-bno
                       move in-perat-area
                                       to perat1-area
                       move in-primary-skill
                                       to perat1-eng-type
                       if in-primary-skill <> spaces
                           move "P" to perat-primary-skill
                       else
                           move "S" to perat-primary-skill
                       end-if
                       
                       //i cant do rewrite here.
                   
   

Solution

  • As you've seen there is no REWRITE of the primary index, because as soon as you change that the REWRITE is not changing the previous read record but the one with the new primary record - if it exists.
    This is how ORGANIZATION INDEXED works.

    The thing that is commonly done:

    • READ old-record (possibly WITH LOCK)
    • if you don't have that already: save the old values of the key fields
    • place the new values in the key fields, then WRITE, ideally with an INVALID KEY clause to catch this case
    • result is one of:
      • INVALID KEY condition (= duplicate): - you have to decide if that should be DELETED (if yes, then re-read the old record again, "update" its key field and do the INSERT again, otherwise abort either with a manual error message or by letting the runtime system handle the duplicate key)
      • NOT INVALID KEY condition - everything is fine
    • place the saved (old) value in the key fields, then DELETE (as you'd otherwise have the record in the file with both the old and the new key values)

    Depending on the rules in your environment you may need to READ with the new key and therefore don't ever get into the INVALID KEY condition during WRITE.