This file will be used for update, it is keyed, and user opened. Line 7: As this is a test program I want to be able to run this multiple times to make sure I update the right field with the right value. Rather than resetting the fields in the file I am going to copy the records from a " saved " file before any other processing.
By using the user open in the file's definition the file remains close until I open it. Which I do here. I can use a string instead. This will leave the value in the other field unchanged.
The danger is in a complex program I could have changed the value in a field in another file with the same name as one in this file. 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.
I will be using the data structure to receive the input from the SQL Fetch. I can do this when compiling the program too, but this makes absolutely sure just in case the person who compiles the program forgets to change the compile command parameter. The Select statement is used to define which columns fields I want, from which file, and the selection criteria in this case just the rows where FKEY is '1', and the sort order.
I always add line 11, it is not necessary as without the cursor is still opened for update. I just think it is better as it documents how the file will be used. Line A fetch is like a read, it retrieve a row record from the file. By using a data structure that is defined to be the same as the file, lines 2 and 3, I can fetch directly into it, rather than have to list individual columns fields. Lines 15 — This is the equivalent of end of file processing. In my experience the most common error is end of file.
Lines 19 — As I only want to update one column field I use SQL's update statement giving the column name I want to change with the value I want to change it to. The part to notice is line 21, this tells the cursor to update the cursor's current row. If I want to update all the columns in the cursor's current row I would change my update statement to be:. Line 20 shows that I am setting the row to be the same as the data structure I fetched the data into.
Any column not included gets the default value. To prevent "comment spam" all comments are moderated. Learn about this website's comments policy here. Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.
The answer is a definite "Yes". I can move values to my subfields and then insert a row using the data structure: 01 Data. Line 5: This variable will be used to retrieve data from the data structure array. Line 7: I move the element from the data structure array into the plain data structure.
Update follows an insert. If I wanted to update the last row I inserted it would look like: 01 Data2. Column1 ; if you notice I did not change the value in the subfield Data2. Update I have Lyle Larsen Jr. This article was written for IBM i 7. Labels: programming , rpg , sql. Anonymous January 17, at AM. Simon Hutchinson January 17, at PM.
Scott January 17, at PM. Simon Hutchinson May 2, at AM. HassanFarooqi February 8, at AM. Already a member? Close this window and log in.
Join Us Close. Join Tek-Tips Forums! Join Us! By joining you are opting in to receive e-mail. Promoting, selling, recruiting, coursework and thesis posting is forbidden. Students Click Here. I can interactively start SQL and run the update and there is not a problem. What does journaling of my file have to do with the SQL statement? The file is not journaled.
0コメント