Page 1 of 2

Updating Record Key

PostPosted: Thu Sep 15, 2011 12:52 am
by crazycobol
Hello everyone. Just a quick question. How do you update the "record key"

Lets say I have a record key that is 20007000 and I want to change that record key to 200080000.

I have tried to do a rewrite however you can not do a rewrite on the record key.

How do I go about updating the record key? is it possible or do I have to delete the entire record and write another record with the correct record key.


The file structure goes something like this:

FD FILE

01 REC

05 KEY
10 CKEY PIC X(8).

05 DATA


Any help is greatly appreciated.

Re: Updating Record Key

PostPosted: Thu Sep 15, 2011 1:06 am
by crazycobol
the record key is for Primary Record Key just in case there was any confusion.

Re: Updating Record Key

PostPosted: Thu Sep 15, 2011 1:13 am
by dick scherrer
Hello,

Suggest you add the record with the new key and then delete the record with the old key - in that order. . .

Re: Updating Record Key

PostPosted: Thu Sep 15, 2011 1:38 am
by crazycobol
Thank you Dick for the reply.

Do I do the following:

move 200080000 to ckey.

write rec.

Then I do:

if ckey equal 20007000
then delete rec.

Re: Updating Record Key

PostPosted: Thu Sep 15, 2011 1:57 am
by BillyBoyo
crazycobol wrote:Thank you Dick for the reply.

Do I do the following:

move 200080000 to ckey.

write rec.

Then I do:

if ckey equal 20007000
then delete rec.



Mmmm... by then, ckey won't be 20007000(0) any longer.

Really, how you have to do it depends on what is driving your progam.

You don't really "change a key" on a file. You do delete the old key, and add the new key.

How do you know you need to change the key? That provides your method. If you are identifying someone from solely the file itself, then conside what Dick has suggested.

If you have some sort of transaction file, exactly what do you have?

So, how do you know the key has changed.... ?

Re: Updating Record Key

PostPosted: Thu Sep 15, 2011 2:33 am
by crazycobol
The file is a dat file that store records. The primary key is used to identified the record. So currently the record in the file has 20007000 as the primary key and then after it has data. The next record would be 20009000 along with data behind it.

so it look like this:

20007000abcdefgdddd
20009000abcdefffffdd


I would like to change the 20007000 record to look like this:

20008000abcdefgdddd

keeping the data intact.

Re: Updating Record Key

PostPosted: Thu Sep 15, 2011 3:52 am
by BillyBoyo
Is there only the one record to change (=add/delete)?

If so, read with the key the record you want to delete. Keep a copy of the key value in WS. Change the key in the file area to what you want it to be. Do the write to add the recored. Re-read the original record using the key saved in WS. Delete that record. Issue some nice messages so that you know what the program has done.

If there are more than one you need to do, I'd make a flat file with the old/new keys and drive the update from that. Same sort of processing thereafter.

Re: Updating Record Key

PostPosted: Thu Sep 15, 2011 5:29 am
by crazycobol
BillyBoyo wrote:Is there only the one record to change (=add/delete)?

If so, read with the key the record you want to delete. Keep a copy of the key value in WS. Change the key in the file area to what you want it to be. Do the write to add the recored. Re-read the original record using the key saved in WS. Delete that record. Issue some nice messages so that you know what the program has done.

If there are more than one you need to do, I'd make a flat file with the old/new keys and drive the update from that. Same sort of processing thereafter.


great. Yes it is only one record to change. I will give it a shot tomorrow. Thank you again for your help. Very much appreciated.

Re: Updating Record Key

PostPosted: Thu Sep 15, 2011 4:51 pm
by BillyBoyo
No problem. Thanks for the thanks, which is shared amongst us. Let us know how it goes.

Re: Updating Record Key

PostPosted: Sat Oct 01, 2011 3:31 am
by crazycobol
sorry for the late response. It works. :)