Search code examples
sqlibm-midrangerpgle

RPG exec sql works only once


I wrote program in RPG IV free-form and i met a problem. Im trying to execute sql statment with:

dcl-proc getRecord;
dcl-pi *N int(5) end-pi;
dcl-s rec int(5) inz(0);

exec sql
select max(SCORE) into :rec from SNK_HISC;

return rec;
END-PROC;   

It works only once after compilation. Maybe i should reset some indicators or sql flags? At the end of program i use:

*inlr = *on;
return;    

Where procedure is called:

   exfmt BACK;
   exfmt INFO;

   inital();


   dow run and alive;
     write MAINBOARD;
     READ(E) AKE_DSP;
     run = FKeyListener();
     alive = goForward();
   ENDDO;
   LSCORE = points;
   if points > getRecord();
     LRECORD = 'NEW RECORD!';

     exec sql
       insert into AKE_HISC( USID , SCORE) VALUES( :userID , :points);

   ENDIF;
   exfmt ENDING;


   *inlr = *on;
   return; 

And in this procedure:

dcl-proc inital;
     snake(1).x = 5;
     snake(2).x = 5;
     snake(3).x = 5;
     snake(1).y = 5;
     snake(2).y = 6;
     snake(3).y = 7;
     direction.x = 1;
     direction.y = 0;
     SCORE = points;
     RECORD = getRecord();
     genSnack();
     refresh();
   END-PROC;

I run program with this CL program:

         PGM

         MONMSG     MSGID(CPF0000 MCH0000) EXEC(GOTO CMDLBL(END))

         OVRDSPF    FILE(AKE_DSP) DEV(*REQUESTER) WAITRCD(1)

         CALL       PGM(AKE_S)

 END:    
         DLTOVR     FILE(*ALL)
         MONMSG     MSGID(CPF0000)

         ENDPGM

Solution

  • Problem was not with sql query but with that where it is. I removed procedure getRecord() and make rec global variable and the sql query was placed inside the main cycle thats all.