Question

AS400 CL programming

Asked by: barrygg

Is it possible to write to a database physical file within a CL program, using data created through a variable e.g. current date value. I wish to write the &MSGDTA variable to a file for future processing.
             PGM                                                    
             DCLF       FILE(BGUNDRY/TESTDUMM)                      
             DCL        VAR(&ACTYEAR) TYPE(*CHAR) LEN(2)            
             DCL        VAR(&MSGDTA) TYPE(*CHAR) LEN(9)              
             DCL        VAR(&QDATE) TYPE(*CHAR) LEN(6)              
             DCL        VAR(&DAY) TYPE(*CHAR) LEN(2)                
             DCL        VAR(&MONTH) TYPE(*CHAR) LEN(2)              
             DCL        VAR(&YEAR2003) TYPE(*CHAR) LEN(4) +          
                          VALUE('2003')                              
             DCL        VAR(&YEAR2004) TYPE(*CHAR) LEN(4) +          
                          VALUE('2004')                              
             RTVSYSVAL SYSVAL(QDATE) RTNVAR(&QDATE)   /* DDMMYY */  
             CHGVAR VAR(&ACTYEAR) VALUE(%SST(&QDATE 5 2))            
             IF ((&ACTYEAR) *EQ '03') THEN(GOTO CALC1)              
             ELSE (GOTO CALC2)                                      
  CALC1: CHGVAR VAR(&DAY) VALUE(%SST(&QDATE 1 2))                    
            CHGVAR VAR(&MONTH) VALUE(%SST(&QDATE 3 2))                
CHGVAR VAR(&MSGDTA) VALUE('D' *CAT &YEAR2003 *CAT &MONTH *CAT &DAY)    
            GOTO SNDMSG                                                
 CALC2: CHGVAR VAR(&DAY) VALUE(%SST(&QDATE 1 2))                      
            CHGVAR VAR(&MONTH) VALUE(%SST(&QDATE 3 2))                
CHGVAR VAR(&MSGDTA) VALUE('D' *CAT &YEAR2004 *CAT &MONTH *CAT &DAY)    
 SNDMSG:    SNDPGMMSG  MSGID(CPF9898) MSGF(QCPFMSG) MSGDTA(&MSGDTA)    
            ENDPGM                                                    
***************** End of data ****************************************

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-04-24 at 08:27:08ID20595233
Tags

as400

,

cl

Topic

Programming for iSeries / AS400

Participating Experts
6
Points
50
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Run CL on AS400 from VB
    How to call CL procedure on AS400 from VB?
  2. Executing AS/400 CL from VB Application
    I am having difficulty calling a simple CL Program on the AS/400 from a VB Application. I have used the IBM Client Access example showing how to retrieve a library description and it works in our environment. I mimiced my VB program after the IBM example and it appears to r...
  3. Submit a batch job in one CL pgm
    I want to call a pgm in batch mode using one CL pgm only, by combining the batch call with the final pgm call. I am passing parms and can do so with a DS or by passing the parms themselves. The programmer before me would always have one CL interact with the screen, call the...
  4. return code in CL script
    Hi Guys, Below piece of code i use it in my CL script where I am running bunch of ILE C executables one after another.The application uses bunch of input and output files. If for any termination from C function below program returns non-zero and CL script stops, but lately d...
  5. Bulk AS400 CL comands
    I am using the Copy File (CPYF) command to copy records from a local file to a remote DDm file. There are a total of 50 files I have to access 10 remote boxes and multiple libraries. is there any way I can put this multiple CL commands in a srcfile and run them as a batch job...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: murphey2Posted on 2003-04-24 at 09:14:22ID: 8389688

Hi barrygg,

I'm almost sure that ther is no option to write a file directly from CL, I needed this more than once in the past years, and searched for it in manuals and internet, but all solutions that I found is writing a simple 5 line RPG program or download some 400/tool that tells you that it can write a record from CL, but actualy including a rpg or cobol or whatevr program that will do it for you.
I don't know if you are familiair with RPG but a RPG program that wil do it can be simple as this:
*************** Beginning of data *****************************************
     FMYFILE  O   F     256            DISK                      A        
     C           *ENTRY    PLIST                                          
     C                     PARM           MSG   256                        
     C                     EXCPT                                          
     C                     SETON                     LR                    
     OMYFILE  EADD                                                        
     O                         MSG                                        
****************** End of data ********************************************

you can call it with call PGM(MYPROG) PARM(&MSGDTA)


I realize this is not the most beautiful way, but you only have to define a file "MYFILE" and that's it.
Of cause you can extend it with external described or diferent length etc. but this is the idea!!


Have Fun

 

by: shalomcPosted on 2003-04-24 at 17:19:18ID: 8392027

I use the following trick when I need data file manipulation from CL.

1. Write a RPG program called EXECSQL that executes dynamic SQL statements.
2. Write a command called EXECSQL to pass a SQL statement to the program.

Now, you can use a CL command like this:

chgvar &insert   ('insert into yourfile values(''' || &MSGDTA || ''')'
execsql stmt(&insert)

Full source is at
http://search400.techtarget.com/tip/1,289483,sid3_gci864804,00.html?FromTaxonomy=%2Fpr%2F2f9


ShalomC

 

by: barryggPosted on 2003-04-25 at 00:41:20ID: 8393398

Thanks

 

by: barryggPosted on 2003-04-25 at 00:41:47ID: 8393401

Thanks

 

by: murphey2Posted on 2003-04-25 at 12:09:21ID: 8397533

Hey ShalomC!!!!!

Great thinking !!!!!!

 

by: shalomcPosted on 2003-04-28 at 07:55:01ID: 8413316

Awwww,
thanks (:=%})

 

by: mmcmillenPosted on 2003-04-29 at 18:39:03ID: 8424835

Right. You can read a file from CL but not update it directly.

 

by: nigel_pettitPosted on 2003-04-30 at 15:08:32ID: 8431947

it is possible to write to a file in CL using commands such as cpyf.(For basic fixed length files(NOT Fields)
You may have to read the file in to calculate the record tehn use cpyf to an temporary file before copying back to replace the original.

Nigel

 

by: CoropunaPosted on 2003-05-08 at 08:20:46ID: 8489357

to store small pieces of information u can use the data areas. these objects r easy to handle within the range of a short cl program.

to create a data area:
CRTDTAARA DTAARA(YourLib/YourDataArea) TYPE(*CHAR) LEN(9)

to get the stored value:
RTVDTAARA DTAARA(YourLib/YourDataArea (1 9)) RTNVAR(&YourVariable)

to store a value:
CHGDTAARA DTAARA(YourLib/YourDataArea (1 9)) RTNVAR(&YourVariable)

note that in this example m using a data area defined as a *char nine bytes long, u can define it almost as long as u want (think about performance) and read specific sections of it.

again, for small pieces of data, within a cl, this is the easiest way.


 

by: shalomcPosted on 2003-07-24 at 05:48:38ID: 8994548

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept shalomc comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

ShalomC
EE Cleanup Volunteer

 

by: tliottaPosted on 2004-11-01 at 17:08:38ID: 12469054

Just saw this thread and thought to add a comment or two...

RPG isn't required for an EXECSQL function; a QM query works fine. Also, in a pinch, CL can call the QLGSORT API -- by supplying the "records" in an input buffer, the CL program can have them written by the API to the sort output file; works perfectly well when you don't have a HLL compiler handy. The QLGSORTIO API can return records if needed.

(The UIM APIs allow you to do screen I/O without using DCLF for a display file. The QSPCRTSP and QSPPUTSP APIs plus user space APIs give spooled print output.)

Tom

 

by: shalomcPosted on 2004-11-02 at 00:24:09ID: 12470706

Hey Tom,
The problem with QM is that you must explicitly add permissions to DML statements like insert and update, to each and every one user profile that will execute this CL. That may be both inconvenient and insecure.

ShalomC

 

by: tliottaPosted on 2004-11-02 at 12:37:48ID: 12477118

ShalomC:

Since that ought to be done anyway even if the RPG program is used, I'm not clear on the point. By using QM to manage those permissions, you avoid giving them to everyone through an uncontrolled RPG program (or complicating the RPG program to include the logic to duplicate what QM does for you).

If such permissions were to be managed by granting authority to the RPG program, the same authority could be granted to the CL that executes the STRQMQRY command.

Note that my comment was only to point out that those of us who cannot afford a HLL compiler can still get the task done. I have no RPG compiler, but CL (and REXX and UIM and most APIs) are available wherever OS/400 runs. Lowest common denominator? Yes, no doubt. But pretty much guaranteed available.

Tom

 

by: shalomcPosted on 2004-11-02 at 14:20:13ID: 12478305

I love REXX.
Initially the generic SQL execution command used REXX as CPP, but I realized that RPG people tend to tread on familiar ground, if you know what I mean.

Shalom

 

by: tliottaPosted on 2004-11-02 at 17:07:54ID: 12479344

Shalom:

My version of RUNSQL has always had a REXX CPP simply because all the others I saw used CL or RPG. I don't recommend it due to the overhead, but I thought it illustrated an alternative. I love REXX too.

My _major_ complaint is that it never was given compiler status under OS/400. Too bad... lots of very cool stuff can be done, including embedded SQL and UIM handling of screens (plus all the APIs available to OPM CL). The embedded SQL doesn't even require the SQL Dev Kit!

I really wish more would use REXX/400 and start demanding enhancement. After all this time, there is still neither an ObjectREXX nor a NetREXX available for OS/400. Pity.

Tom

p.s. I've been looking at STRQM profile management because of this thread. At least on the V5R1 system I've been running some tests on, the statement restrictions are _not_ being applied. I.e., I have myself limited to SELECT and DELETE, but can send INSERTs into my QM query with no errors. Yow. Time to verify with IBM what the real deal is.

 

by: shalomcPosted on 2004-11-03 at 07:57:31ID: 12484276

Hey Tom,
I just found my own version of the REXX generic SQL routine.
It does not support regular SELECT statements, but only action statements.
I had plan to add SQL syntax verification, but never got to it.

Command:
EXECSQL:     CMD        PROMPT('Execute SQL from CLP ')                        
                                                                               
             PARM       KWD(STMT) TYPE(*CHAR) LEN(4000) MIN(1) +              
                          CASE(*MIXED) PROMPT('SQL command')                  
             PARM       KWD(COMMIT) TYPE(*CHAR) RSTD(*YES) DFT(*CHG) +        
                          SPCVAL((*CHG UR) (*NONE NC) (*ALL RS) +              
                          (*UR UR) (*NC NC) (*RC RC)           (*RR +          
                          RR)  (*RS RS) ) PROMPT('Commitment Control')    


REXX:
/*       By  Shalom Carmel, 2001                                         */
/* This REXX script is the CPP of EXECSQL command                        */
/* It's purpose is the arbitrary execution of SQL code in a CLP program  */
/*                                                                       */
/*                                                                       */
                                                                           
    signal on error name command_error                                      
    signal on failure name command_error                                    
    signal on syntax  name command_error                                    
                                                                           
parse arg lower 'STMT(' QUOTE_SQL_statement ')'                            
parse arg       'COMMIT(' CommitControl ')'                                
select                                                                      
  when CommitControl='*CHG'  then Commitment='UR'                          
  when CommitControl='*UR'   then Commitment='UR'                          
  when CommitControl='*ALL'  then Commitment='RS'                          
  when CommitControl='*RS'   then Commitment='RS'                          
  when CommitControl='*RC'   then Commitment='RC'                          
  when CommitControl='*RR'   then Commitment='RR'                          
  when CommitControl='*RR'   then Commitment='RR'                            
  when CommitControl='*NONE' then Commitment='NC'                            
  when CommitControl='*NC'   then Commitment='NC'                            
  otherwise                  Commitment='NC'                                
end                                                                          
interpret 'SQL_statement= '   QUOTE_SQL_statement                            
   ADDRESS  EXECSQL                                                          
     'EXECSQL  SET TRANSACTION ISOLATION LEVEL ' Commitment                  
     'EXECSQL '  SQL_statement                                              
                                                                             
                                                                             
exit                                                                        
 /********************************************************************/      
 /*  command_error : ERROR & FAILURE condition trap                  */      
 /********************************************************************/      
/* The RC variable contains the actual error message from the AS400 system */
/* This routine can be customized for the specific implementation         */
 command_error:                                                              
parse source _system _start _srcmbr _srcfile _srclib                        
/*    Available SQL information:                                            
     SQLCODE                                                              
     SQLERRMC                                                              
     SQLERRP                                                              
     SQLERRD.1                                                            
     SQLERRD.2                                                            
     SQLERRD.3                                                            
     SQLERRD.4                                                            
     SQLERRD.5                                                            
     SQLERRD.6                                                            
     SQLSTATE                     */                                      
errmsg = _srcmbr 'from' _srclib'/'_srcfile 'has failed: SQLCODE ' SQLCODE  
                                                                           
   ADDRESS  COMMAND                                                        
'SNDPGMMSG MSGID(cpf9899) MSGF(QCPFMSG) TOPGMQ(*PRV) MSGTYPE(*info) '      
'SNDPGMMSG MSGID(cpf9897) MSGF(QCPFMSG) TOPGMQ(*PRV) MSGTYPE(*escape) ' ,  
   'MSGDTA('''errmsg''')'                                                  
 exit                                                                      

 

by: tliottaPosted on 2004-11-05 at 19:06:13ID: 12511088

A possible toy for playing with SQL syntax checking:

 SYNSQL:     CMD        PROMPT('Syntax check SQL statement')

             PARM       KWD(SQL) TYPE(*CHAR) LEN(512) MIN(1) +
                          EXPR(*YES) VARY(*YES) PROMPT('SQL statement')

...and:

pgm         ( +
              &pSQL       +
            )

   dcl        &pSQL             *char      514


   dcl        &SQL              *char      256
   dcl        &RecLen           *char        4
   dcl        &NbrRec           *char        4
   dcl        &Options          *char      256
   dcl        &SQLStmI          *char      512
   dcl        &LenSQLStmI       *char        4
   dcl        &NbrRecPrc        *char        4
   dcl        &Error            *char        8
   dcl        &RecLen_D         *dec      (  5 0)

   dcl        &LenErrI          *dec      (  5 0)

   dcl        &SQLMsgF          *char       10
   dcl        &SQLMsgFLIB       *char       10
   dcl        &LENMsgI          *dec      (  5 0)
   dcl        &MsgID            *char        7
   dcl        &SQLSTATE         *char        5
   dcl        &LenMsgTxt        *dec      (  3 0)
   dcl        &MsgTxt           *char      256
   dcl        &LenRplTxt        *dec      (  3 0)
   dcl        &RplTxt           *char      256


   chgvar           &SQL                 %sst( &pSQL 3 512 )
   chgvar     %bin( &RecLen 1 4 )        %bin( &pSQL 1 2 )
   chgvar     %bin( &NbrRec 1 4 )      ( 1 )
   chgvar     %bin( &Options 1 4 )     ( 0 )
   chgvar     %bin( &LenSQLStmI 1 4 )  ( 512 )


   call       QSQCHKS     ( +
                            &SQL         +
                            &RecLen      +
                            &NbrRec      +
                            '*NONE'      +
                            &Options     +
                            &SQLStmI     +
                            &LenSQLStmI  +
                            &NbrRecPrc   +
                            &Error       +
                          )


   chgvar           &LenErrI             %bin( &LenSQLStmI )

   chgvar           &SQLMsgF             %sst( &SQLStmI 1 10 )
   chgvar           &SQLMsgFLIB          %sst( &SQLStmI 11 10 )

   chgvar           &LenMsgI             %bin( &SQLStmI 25 4 )
   chgvar           &MsgID               %sst( &SQLStmI 53 7 )

   chgvar           &SQLSTATE            %sst( &SQLStmI 60 5 )

   if ( &SQLSTATE *ne '00000' )      do

      chgvar        &LenMsgTxt           %bin( &SQLStmI 65 4 )
      chgvar        &MsgTxt              %sst( &SQLStmI 69 &LenMsgTxt )
      chgvar        &LenRplTxt           %bin( &MsgTxt 1 2 )
      chgvar        &RplTxt              %sst( &MsgTxt 3 &LenRplTxt )

      sndpgmmsg     msgid( &MsgID )  +
                      msgf( &SQLMsgFLIB/&SQLMsgF ) +
                      msgdta( %sst( &MsgTxt 1 &LenMsgTxt )) +
                      msgtype( *INFO )

   enddo


   return

endpgm

Plenty of enhancements can be made, but maybe it'll help on your way.

Tom

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...