i'm trying to populate a field in A table with the row number using rrn(tablename). the first time if i insert 4 rows then the id is 1,2,3,4 as required. if i delete the rows and try the insert again the rrn() does not return the same so 1,2,3,4 but returns 5,6,7,8 (that is the nr of rows deleted + 1). how can i fix this?
thnx a lot.
elai
Software/Hardware used:
as400/DB2,V5R4M0
ASKED:
November 19, 2010 2:37 PM
UPDATED:
November 23, 2010 8:54 PM
Note that you can use OVER () by itself, i.e., no parameters, if you are ordering by arrival sequence. That is, the query in the ‘Answer’ should run exactly as written for a table named MYTABLE.
Tom
thank you for your answers.
the problem is that all the records in this table are added and deleted daily,
and the field i want to populate should always begin from 1, so the auto-increment field does not solve my problem.
i have used rrn() in the past and have never had this problem????
i´m using cl programming.
if i run RGZPFM i fix the problem or i should do something else?
should i run this after the delete query and before the insert ?
thnx
elai
i have used rrn() in the past and have never had this problem?
RRN() cannot be guaranteed to return row numbers beginning with 1, as you are seeing. If you look at the file with DSPFD and use TYPE(*ATR), you should able to scroll down and see the ‘Reuse deleted records’ (REUSEDLT) attribute. It should be near the bottom of the second screen or top of the third.
This file appears to have REUSEDLT(*NO). If you made RRN() work in the past, those files probably had REUSEDLT(*YES). If not, then they didn’t have records that were deleted — they had members that either were cleared with CLRPFM or had been reorganized with RGZPFM. (When all rows are deleted with SQL DELETE without a WHERE clause, SQL can use CLRPFM.)
You can use CHGPFM REUSEDLT(*YES) against your current file to change it to use the deleted record spaces over again.
Why does it matter if RRN() returns a 1 or a 5 for the first row? The only thing RRN() is intended to do is tell you what the order is. As long as record 5 comes before 6, and 6 comes before 7, etc., it shouldn’t matter if the counting starts at 1 or at 5. The order is still the same.
Relying on physical record order instead of logical order is likely to introduce problems at unexpected times.
Tom
Yes, if you want to use the RGZPFM it would be between the delete query and the call to the RPG program.
Perhaps it’s worked ikn the past because your jobs created a duplicate work file in QTEMP for the run. Since it was a new file the RRN() would be 0. But if you know that you have a cleared file and you want to start at 1 why use RRN()?
Phil