简体   繁体   中英

How do i run an SQL in a CL program (AS400)

( Let me preface this by saying I am extremely new to the AS400 and RPGLE and CL Programming. I know SQL fairly well, but not when in use with the AS400 )

This seems like it should be very simple.

At Year End we have to update our warehouse table to the new current year. This has been a manual process, to go to STRSQL and run

 update rco set ccfscy = '2017' where crsts = 'A'

This has been done by the same person for the past 40 years and they are now retiring this year. My boss wants this to become a CL Program where it is all done without someone manually running the STRSQL command and it can be integrated into the Year End Process.

All the CL Program has to do is prompt for what year you want to change the warehouse files too and upon entering the date into the screen it would run the SQL program and update the records according to the SQL command.

Can someone please point me into a direction where I might be able to learn more about using the CL program to accomplish this process?

Thank you.

Thomas J Cusick, Systems Programmer

Assuming you're on a currently supported version of IBM i..

IBM added a RUNSQL CL command a few versions ago...

A complete CL program would thus look like so:

pgm
  runsql sql('update rco set ccfscy = ''2017'' where crsts = ''A''')
endpgm

But it'd be better if you passed in the year as a parameter...

pgm parm(&year)
  dcl &year *char 4
  dcl &stmt *char 50
  /* using &quote makes life easier than doubling quote literals */
  dcl &quote *char 1 value('''')

  chgvar &stmt value('update rco set ccfscy =' *cat &quote *cat &year *cat +
     &quote *cat 'where crsts = ' *cat &quote *cat 'A' *cat &quote)

  runsql sql(&stmt)

endpgm

A very simple example using RPGLE and DDS instead of CL or QMQRY would look like this:

The display file:

 A                                      INDARA
 A          R DSPLY                     WINDOW(*DFT 7 35)
 A                                      WDWTITLE((*TEXT 'Year End Process')-
 A                                       *TOP *CENTER)
 A                                      CF03(03)
 A                                  2  2'Type values, press Enter.'
 A                                      COLOR(BLU)
 A                                  4  5'Current Year :'
 A                                      COLOR(WHT)
 A            CURYR          4Y 0B  4 22EDTCDE(4)
 A  30                                  ERRMSG('Invalid Year')
 A                                  6  2'F3=Exit'

RPGLE

   ctl-opt Option(*SrcStmt : *NoDebugIo: *NoUnref)
           DftActGrp(*No) ActGrp(*New)
           Main(main);

   // display file and indicators
   dcl-f prompt Workstn Indds(Indicators);
   dcl-ds Indicators Len(99);
     F3Pressed      Ind Pos(3);
     InvalidYear    Ind Pos(30);
     Errors         Ind Pos(30) Dim(10);
   end-ds;

   dcl-proc main;

     dcl-s ContinueTransaction   Ind Inz(*On);
     dcl-s CurrentYear           Int(5) Inz(0);

     CurrentYear = %subdt(%date(): *YEARS);
     curyr = CurrentYear;
     dow ContinueTransaction;
       exfmt dsply;
       ContinueTransaction = *Off;
       Errors = *Off;
       select;
         when F3Pressed;
         when curyr >= CurrentYear - 1 and
              curyr <= CurrentYear + 1;
           exec sql
             update rco
               set ccfscdy = :curyr
               where crsts = 'A';
         other;
           InvalidYear = *On;
           ContinueTransaction = *On;
       endsl;
     enddo;

     close prompt;
     return;
   end-proc;

This defaults the current year into the prompt, and provides a message when the entered year deviates too far from the current year. Not as quick and dirty as the QMQRY solution, but really not all that complex either, and once you have it, you can copy and change the prompt, validation, and SQL to your hearts content.

Thanks Charles, this helped... In the mean while my boss came to me with an example of what he actually was looking for and the following is what I ended up doing.

First I created a File CLD@91Q1 of Type *QMQRY, this file holds the actual SQL statement...

UPDATE &GWHRE.RCO SET CCFSCY = &SELYEAR WHERE CRSTS = 'A'

Second I created a XCLP that calls the CLD@91Q1 Query

...

/*********************************************************************/  
/* CALL SQL TO UPDATE THE COMPANY RECORDS WITH THE CURRENT YEAR      */  
/*********************************************************************/  

         CHGVAR     VAR(&SELYEAR)   VALUE(&GYEAR)                    
         STRQMQRY   QMQRY(*LIBL/CLD@91Q1) NAMING(*SYS) +             
                      SETVAR((SELYEAR &SELYEAR) (GWHRE &GWHRE))      
...

and Finally I created the XDSPF file to give the user a place to enter the needed information.

Looking at this it appears to be kinda convoluted but this is what the Boss wanted so this is what the boss got.

I think it's a good exercise and I can see using this kind of process in more complex SQL statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM