Updating row selected by cursor
I always add line 11, it is not necessary as without the cursor is still opened for update.
Mine looks like: 01 **free 02 dcl-pr QCMDEXC extpgm ; 03 *n char(100) options(*varsize) const ; 04 *n packed(15:5) const ; 05 end-pr ; 06 dcl-f TESTFILE usage(*update) keyed usropn ; 07 QCMDEXC('CPYF FROMFILE(TESTFSAVE) TOFILE(TESTFILE) MBROPT(*REPLACE)':56) ; 08 open TESTFILE ; 09 setll ('1') TESTFILER ; 10 dow (1 = 1) ; 11 reade ('1') TESTFILER ; 12 if (%eof) ; 13 leave ; 14 endif ; 15 FFIELD = 'A' ; 16 update TESTFILER %fields(FFIELD) ; 17 enddo ; 18 close TESTFILE ; 19 *inlr = *on ; running 7.3 I am going to use totally free RPG.If I had changed many fields in the file rather than listing them all in the 01 **free 02 dcl-ds Data Ds extname('TESTFILE') 03 end-ds ; 04 exec sql SET OPTION COMMIT = *NONE ; 05 exec sql CALL QSYS2.QCMDEXC('CPYF FROMFILE(TESTFSAVE) TOFILE(TESTFILE) MBROPT(*REPLACE)') ; 06 exec sql DECLARE C0 CURSOR FOR 07 SELECT * 08 FROM TESTFILE 09 WHERE FKEY = '1' 10 ORDER BY FKEY, FFIELD 11 FOR UPDATE ; 12 exec sql OPEN C0 ; 13 dow (1 = 1) ; 14 exec sql FETCH C0 INTO : Data DS ; 15 if (SQLCOD Lines 2 - 3: I know in this example my file only has two fields, but I like to code my examples to cover more complex scenarios.By using the user open in the file's definition the file remains close until I open it. Line 9: This operation shows that I don't have to use a variable for the key field. Lines 10 – 17: My Do loop to read all of the records in the file.
Line 11: I can also use a string for the key when using the built in function.The part to notice is line 21, this tells the cursor to update the cursor's current row.