Insert records between records in a file using RPG code.

SQL scripts
Is there a way to insert a record in between records in a file? For example, a file contains 10 records (1 to 10). I want to insert a record in between records 1 and 2, thus record 2 will move to position 3, 3 to 4, and so on and so forth. Can SQL/Insert function be used to do this?

Answer Wiki

Thanks. We'll let you know when a new response is added.


I don’t think there’s an easy way to do this, but there are several options.

You could try defining your file with a key.
You could try reorganizing your file with a key.
You could copy the records before the one you need to add to a temporary file, add your record to the temporary file, then copy the records after the record you need to add to the temporary file, then copy them all back.
You could also write an RPG program to allow this.

Maybe it would help if we knew what you’re trying to achieve by this.


Martin Gilbert.

Discuss This Question: 2  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Francissamonte
    Hi Martin, Thank you for your prompt response. Actually, I am creating a utility to tidy up RPGLE source code such as putting a header (program id, description, author, date, modification log, etc.) , adding some colors to the comment lines, adding comments to the comment fields(pos 80) for endif, enddo etc operation code. I have managed to do these except for inserting header lines at the beginiing of the source. Now, I am trying the 3rd option you have suggested but instead of using a temporary file, I opt to use a multiple occurence DS to keep all the records, and if the source doesnt have a header, the program will create header (retrieves from a table) and copy back all records from the data structure using OCCUR opcode. But I havent figured out what will happen to the sequence number and date. In SEU, there is an insert option (I) to insert lines of code. I wonder how IBM is doing that. Regards, Francis
    15 pointsBadges:
  • Sloopy
    Hi, Francis. In SEU, the source code is copied into a space and manipulated by the editor. When you exit SEU, the space is read, and its data relpaces the data in the source physical file member. What you want to do is actually quite common. Usually it's done by writing the data to a temporary source member, or even a temporary database file, and then replacing the original source member with the manipulated data. Whatever way you find easiest to manipulate the source lines is good, although some methods are easier than others. Multiple-occurrence data structures are probably not the best, since you have no 'key' information you can use to organise your source lines. However, as a method it would work. Do you want to insert your header information without having to resequence the rest of your source? So long as your source sequences are not fractional (i.e. they are 0001.00, 0002.00, 0003.00 etc.), you can insert 99 lines between your source lines, or at the top of the source (0000.01 - 0000.99). If you want to resequence your source when you put it back in the source member, then you first need to know how many source records you have - if you have more than 9,999 you'll have to resequence in fractions. But this would be unusual with modern languages. Anyway, resequencing is just a matter of starting with 0001.00 (or 0000.10) and incrementing by 1 (or 0.1) for each record written, in source order. You need to fully replace the original source member from your stored amended version, since you can't insert a physical record into a source member between two existing records, and source members are not read keyed by compilers; so, if you add a few records at the end of the source with sequence numbers which should put them at the beginning of the source, you will get an SEU warning when you open that member in SEU, and you will get some lovely errors when you try to compile the source. Lastly, the source change date is a field in the source record. When you load your temporary file, space, array or M/O data structure with source records you will have three fields for each record - the sequence number, the date and the source data. If your program changes a source record, it can change the data for that record. If it inserts source records, it can date these new records. If you change the sequence number of a source record, there is no need to change the source change date, unless you want to. And when you read your data back into a source member, you read back the source date as well as the source line. Regards, Sloopy
    2,195 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: