Read selective file in CL by using OVRDBF

195 pts.
Tags:
AS/400
CL Program
OVRDBF
How do I read selective file in CL by using OVRDBF by positioning to from particular record till selective record?
Lets say I have Emp (record format is REC) file which is having 10 records (rrn or key field). Now I want to read file from 2nd record till 8th  record, how can we do this with OVRDBF?
I think we can achieve this with the help of *position but I'm failing. Please can someone help me.
1

Answer Wiki

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

There are more efficient ways to do this by using SQL or OPNQRYF.

IF you have to use CL and the OVRDBF then I’d try something like this
OVRDBF     FILE(RRNFILE) TOFILE(MYLIB/MYFILE)
             POSITION(*RRN 2)                

the set up a do loop and do a RCVF until you have read the desired number of records.

Discuss This Question: 15  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.
  • TheRealRaven
    It's not clear exactly what you need. I suppose you could create a loop variable that you increment FROM(2) TO(8), and then use POSITION() for each record.

    But using RRN implies "no deleted records" in the file. That applies to the very first record you try to read as well as to every subsequent read. You might not read the record you expect, and you might read the same physical record multiple times.

    What isn't clear is why you'd need to do this. If we knew what your problem was, it might be possible to come up with useful answers.
    36,035 pointsBadges:
    report
  • hndajad
    Thank you raven for your response, My need is very simple i just want to read the mindle part of file as i said from 2 to 8th record, just want read no delete operation is required. i want to this with the help of OVRDBF.i know how to do this in RPG but not aware in CL.. 
    195 pointsBadges:
    report
  • hndajad
    Thank u todd, Todd could you please place that do loop line as well along with RCVF. Please

    i tried but its not working from my side,
    195 pointsBadges:
    report
  • TheRealRaven
    Show us what you have tried, and we'll see what might be wrong.

    I didn't say there would be any "delete operation". I said that RRN can't be assumed to give accurate records if you want to read certain records once any records have ever been deleted from the file. You should only use RRN to locate relative positions in a file. If you position to RRN #2, the next record that you read might not be RRN #2. It might be #3 or #1000. It might be any RRN that is #2 or higher.

    If you want records 2 through 8, you should just start at the beginning and count records. That's true whether you do it in CL or RPG or any other language. You shouldn't use OVRDBF POSITION().
    36,035 pointsBadges:
    report
  • philpl1jb

    Once the dclf is active, POSITION and OVRDBF cannot take effect

    Easiest way to do this is with two CL's

    One issues the ovrdbf then calls the one that has a DCLF command.

    The constraints of this assignment seem so arbitrary as to be homework.

    54,090 pointsBadges:
    report
  • TheRealRaven
    As of IBM i 6.1, CL can use CLOSE to make OVRDBFs effective multiple times. Since i 6.1 has been the oldest supported release for a couple years and no release was specified in the OP, shouldn't we assume it as a minimum?
    36,035 pointsBadges:
    report
  • philpl1jb

    Thanks .. so you would have to:

    issue a close

    then ovrdbf

    then open

    then rcvf

    54,090 pointsBadges:
    report
  • TheRealRaven
    I haven't actually tried it for this kind of problem, so I'm not certain. But it's worked in related problems I ran into. I guess it'd be easy enough to try for this, and I'd be pretty disappointed if it didn't work.

    I'm also curious why this problem even exists (except homework/interview, of course).
    36,035 pointsBadges:
    report
  • hndajad

    Hi Revan here is my code,

                   PGM        PARM(&NAME)                                                 
                   DCL        VAR(&NAME) TYPE(*CHAR) LEN(10)                              
                   DCLF       FILE(ICIDATTA/STUDPF2)                                      
                   OVRDBF     FILE(STUDPF2) POSITION(*KEY 1 STUDENT 105)                              
       READ:                                                                              
                   RCVF                                                                   
                   MONMSG     MSGID(CPF0864) EXEC(GOTO CMDLBL(END1))                      
                   SNDUSRMSG  MSG('The student number is' *cat &STUDNAME)                 
                   IF         COND(&STUDNO *GE 113) THEN(GOTO CMDLBL(END2))               
                   GOTO       CMDLBL(READ)                                                
                   DLTOVR     FILE(STUDPF2)                                               
       END2:                                                                              
                   SNDUSRMSG  MSG('The values is greater')                                
       END1:                                                                              
                   SNDUSRMSG  MSG('End of file')                                          
       ENDPGM                                                                             

    195 pointsBadges:
    report
  • hndajad

    i am not able to set the position record 105, i tried many ways but its not working the error is-" Position is not valied for studpf"

     

    195 pointsBadges:
    report
  • hndajad

    here is error-

    OVRDBF FILE(STUDPF2) POSITION(*KEY 1 STUDENT 105)   

     rest of the all codes are working fine, except hightlited one

     

    195 pointsBadges:
    report
  • TheRealRaven
    That OVRDBF is not coded to position to record 105. It attempts to position to the record that has an index key value of 105. You are specifying *KEY, and not *RRN.

    What is the definition of the key field?
    36,035 pointsBadges:
    report
  • hndajad
    Hi Revan, i tried with *RRN too, but getting same error, my file has only one key field that is studno starting from value 100.
    195 pointsBadges:
    report
  • sodhikar
    hndajad. how can we read file from 2 record to 8 record in rpg?
    10 pointsBadges:
    report
  • Kumar1
    Hi this can be done with the OVRDBF using CL. As mentioned the requirement is to read to read the record from 2nd till 8th.
    We need to check that data base file should have more then 8 records. This will be checked using the counter in the loop and using CPF0864 for end of file..Below are the steps of the coding.
    1) Declare the variable EOF (End of File).
    DCL VAR (&EOF) Type(*CHAR) VALUE('0')
    2) Declare the data base file suppose the file is Employee in library Test.
    DCLF FILE(TEST/EMPLOYEE)
    3) Declare the Display file where you need to display the records.
    DCLF FILE(TEST/EMPDSP) with ID(F2)
    4) Declare the override file using the position option. Define the *RRN and its value 2.
    Note:- If you are using the *RRN then your data base Employee file should not contain the UNIQUE and KEY Filed value. If you have define  like K EMPID in the Employee
    then *RRN will not work and will throw the error Position is not valid.
    OVRDBF (EMPLOYEE) POSITION (*RRN 2)
    5) use the RCVF
    RCVF
    6) Define immediately MONMSG
    MONMSG CPF0864 Then CHGVAR (&EOF = '1')
    7) use the DOWHILE loop
    DOWHILE (&EOF *EQ '0')
    Check for the counter value if > 8 come out of the loop. Do this yourself.
    8) Now move the value from data base to screen data base variables.
    CHGVAR (&F2_EMPNO = &EMPNO)CHGVAR (&F2_EMPNAME = &EMPNAME)
    Here left side are screen variable and right side are data base .
    9) SNDRCVF
    10 increment the counter value
    11 RCVFAgain add mommsg as above
    12 ENDDo
    13ENDPGM

    30 pointsBadges:
    report

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.

Following

Share this item with your network: