Sorry, you do not have permission to ask a question, You must login to ask a question.

Sorry, you do not have permission to ask a question.

brainchime.com

brainchime.com

brainchime.com Navigation

  • Home
  • About Us
  • Contact Us

Mobile menu

Close
  • Home
  • Categories
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags

brainchime.com Latest Questions

Admin
Admin
Asked: October 9, 20242024-10-09T20:21:32+05:30 2024-10-09T20:21:32+05:30In: SQL

How to call sql stored procedure with output parameter from within a cl program?

I have been looking for an answer to this for a long time, but I have not found a solution without RPG/COBOL yet.

Is there any way or approach that I use to make a call to the SQL stored procedure from within a CL program?

as400control-languagedb2stroed procedure
  • 0
  • 0
  • 66
  • 9
  • Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp

Related Questions

  • what is level check error and how to resolve level check error?
  • What are AS400 systems. Please provide a introduction of AS400 systems?
  • Is there any online server available for practicing on AS400 system?
  • What are the menus available in AS400?
  • What is subsystem in AS400?
  • How to copy a save file from IFS to a library?
  • How to transfer savf from as400 to pc?
  • How to copy ifs file to another directory?
  • How to copy savf from ifs to pc?
  • How to copy ifs file to physical file?
  • How to copy save file to ifs?
  • How to copy spool file to ifs?
  • How to copy physical file to ifs?
  • How to copy file from pc to IFS?
  • How to download spool file from AS400?
  • How do I delete a library in AS400?
  • How do I copy data from AS400 to excel?
  • What is library in AS400?
  • How to find all the source physical file available in AS400?
  • How to find all libraries in AS400?
  • How to change the library list in AS400?
  • What is access path in AS400?
  • What is the difference between source physical file and physical file in as400?
  • how to find the source file of an object in as400?
  • how to change record length of source physical file in as400?
  • What is cpf4174 error in as400?
  • What is the use of varying keyword in rpgle?
  • What is DDS in AS400?
  • What is the difference between PF and LF in as400?
  • Why do we use CHGPF command in AS400?
  • how to create physical file in as400?
  • What are the data types supported by physical files in AS400?
  • how to add data in physical file in as400?
  • how to view journal entries in as400?
  • what is the use of ovrdbf in as400?
  • What is an array in AS400?
  • what is a data queue in as400 and why do we use data queue?
  • How to run stored procedure in AS400?
  • How to resolve session and device error in AS400?
  • how to check as400 system values?
  • How to check triggers on a file in as400?
  • How to find damaged objects in AS400?
  • what is module in as400?
  • How to create binding directory in as400?
  • how to create ifs folder in as400?
  • What is ASP in AS400?
  • What is JOBQ and how to create a JOBQ in AS400?
  • What is PSDS in AS400?
  • What is SEU in AS400 and why do we use it?
  • What is the multi-format logical file in AS400?
Leave an answer

Leave an answer
Cancel reply

Browse
Browse

Choose from here the video type.

Put Video ID here: https://www.youtube.com/watch?v=sdUUx5FdySs Ex: "sdUUx5FdySs".

6 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2024-10-09T21:12:18+05:30Added an answer on October 9, 2024 at 9:12 pm

    Hi, I used your SQL stored procedure example and just modified your CL program and tried to call a SQL stored procedure the same way as we call any RPGLE or CL program, i.e. Using the CALL command as below:

    pgm                                                    
    dcl        var(&in1) type(*char) len(20) value('name1')
    dcl        var(&in2) type(*char) len(1)  value('M')    
    dcl        var(&out) type(*char) len(1)                
                                                           
    CALL       PGM(STORED33A) PARM((&IN1) (&IN2) (&OUT))   
    return                                                 
    endpgm    

    I compiled using option 14 and called the CL program from the command line and this time I was getting an error MCH3601 received by procedure STORED33CL. (C D I R).

    I took F1 on the error and below is the additional message information that I can see. So, this makes me aware that we cannot call the SQL stored procedure using the CALL command in CL program.

                            Additional Message Information                        
                                                                                  
    Message ID . . . . . . :   CPA0702       Severity . . . . . . . :   99        
    Message type . . . . . :   Inquiry                                            
    Date sent  . . . . . . :   08/19/24      Time sent  . . . . . . :   22:18:07  
                                                                                  
    Message . . . . :   MCH3601 received by procedure STORED33CL. (C D I R)       
    Cause . . . . . :   ILE Control language (CL) procedure STORED33CL in module  
      STORED33CL in program STORED33CL in library EASYCLASS1 detected an error at 
      statement number 0000000600.  Message text for MCH3601 is: Pointer not set  
      for location referenced.  Use F10 (if available) or the Display Job Log     
      (DSPJOBLOG) command to see the messages in the job log for a more complete  
      description of what caused the error.  If you still are unable to solve the 
      problem, please contact your technical support person.                      
    Recovery  . . . :   This inquiry message can be avoided by changing the       
      procedure. Monitor for the error (MONMSG command) and perform error recovery
      within the procedure.  To continue, choose a reply value.                   
    Possible choices for replying to message . . . . . . . . . . . . . . . :      
                                                                           More...
    
      • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
    • Admin
      Admin
      2024-10-09T21:13:43+05:30Replied to answer on October 9, 2024 at 9:13 pm

      Yes, MCH3601 is a known error for the IBM i SQL stored procedure. Please note that the SQL stored procedure cannot be called from the command line interface using the CALL command. That’s why you are getting an MCH3601 error.

      You will encounter this MCH3601 error when you do not qualify the SQL stored procedure CALL statement through an SQL interface. If the call is not qualified and the SQL path is not set to resolve the SQL stored procedure, this MCH3601 error will occur.

      You can only call the SQL stored procedure with the SQL CALL statement.

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
  2. 2024-10-09T21:07:19+05:30Added an answer on October 9, 2024 at 9:07 pm

    Hi, I have written a SQL stored procedure with output parameters as follows:

    CREATE OR REPLACE PROCEDURE STORED33A(                 
            IN P_NAME CHAR(20),                            
            IN P_GENDER CHAR(1),                           
            OUT SUCCESS CHAR(1)                            
             )                                             
    SPECIFIC STORED33A                                     
    BEGIN                                                  
    DECLARE SQLSTATE CHAR(5) DEFAULT ' ';                  
    DECLARE SQLCODE INTEGER DEFAULT 0;                     
    INSERT INTO PF8_D(NAME,GENDER) VALUES(P_NAME,P_GENDER);
    IF SQLCODE = 0 THEN                                    
      SET SUCCESS = '1';                                   
    ELSE                                                   
     SET SUCCESS = '0';                                    
    END IF;                                                
    END   

    This SQL stored procedure STORED33A takes 2 input parameters and one output parameter. It inserts data in PF8_D and returns a success or failure in the output parameter.

    I have compiled it using the RUNSQLSTM command with the following compile options.

    RUNSQLSTM SRCFILE(TESTLIB/STOREDPROC)
              SRCMBR(STORED33A)              
              COMMIT(*NONE)                 
              DBGVIEW(*SOURCE)      

    I then wrote a CL program stored33cl to call this SQL stored procedure STORED33A with output parameter using datalink file manager DB2 CLP program (QZDFMDB2) present in the QSYS library.

    pgm                                                       
    dcl        var(&cmd) type(*char) len(200)                 
    dcl        var(&in1) type(*char) len(20) value('name1')   
    dcl        var(&in2) type(*char) len(1)  value('M')       
    dcl        var(&out) type(*char) len(1)                   
                                                              
    CHGVAR     VAR(&CMD) VALUE('CALL +                        
                 TESTLIB.STORED33A(''' *CAT &IN1 *TCAT +   
                 ''' , ''' *CAT &IN2 *TCAT ''', ''' *CAT +    
                 &OUT *TCAT ''');')                           
    call       pgm(qzdfmdb2) parm((&cmd))                     
                                                              
    return                                                    
    endpgm              

    Compiled CL program with Create bound CL program (CRTBNDCL) command invoked using option 14 on CL source member

    Then I called my CL program stored33cl and then I got an error.

     **** CLI ERROR *****                                                        
             SQLSTATE: 42886                                                     
    NATIVE ERROR CODE: -469                                                      
    IN, OUT, or INOUT not valid for parameter 3 in procedure STORED33A in TESTLIB.                                                                         
    Press ENTER to end the terminal session.          

    Parameter 3 is the output parameter in the SQL stored procedure. Therefore, calling a SQL stored procedure containing an OUT parameter returns SQL0469 i.e. sqlcode: -469, sqlstate: 42886.

    I am not getting how can I call SQL stored procedure in CL program with the output parameter. Can anyone help here and please let me know what I am missing here?

      • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  3. Admin
    Admin
    2024-10-09T20:39:00+05:30Added an answer on October 9, 2024 at 8:39 pm

    I have tried to call the SQL stored procedure with output parameter within a CL program using QZDFMDB2 to run SQL interactively in a CL program and I have succeeded a lot in that. Now, I can call the SQL stored procedure with the output parameter in the CL program, the only drawback that I face is I am not getting the output parameter value returned in my CL program.

    To call the SQL stored procedure with output parameter in the CL program I have used the, so I created a SQL variable and passed that as a return parameter in the form of a command string to program QZDFMDB2 that allows to run SQL statements in CL program. This makes my first work done i.e. calling a SQL stored procedure in a CL program with output parameters which earlier seemed very difficult or almost impossible. So congrats for this success that we have achieved so far.

    Now, the turn is for receiving the return value in the output parameter which is  SQL variable here. So, it was sad but we are not getting return value from SQL stored procedure in our CL program. So, I decided to use some tricks and tweaks so that I would anyway receive the return value or value returned by the output parameter   SQL stored procedure in my CL program. Let me show you the code and steps that I followed to achieve this:

    1. Create a SQL variable using the CREATE VARIABLE statement from STRSQL.
      create or replace variable out char(1) default ' '
    2. Write a SQL stored procedure that returns the output parameter.
      CREATE OR REPLACE PROCEDURE STORED33(                    
              IN P_NAME CHAR(20),                              
              IN P_GENDER CHAR(1),                             
              OUT SUCCESS CHAR(1)                              
               )                                               
      SPECIFIC STORED33                                        
      BEGIN                                                    
      DECLARE SQLSTATE CHAR(5) DEFAULT ' ';                    
      DECLARE SQLCODE INTEGER DEFAULT 0;                       
                                                               
      DECLARE STMT CHAR(500) DEFAULT ' ';                      
                                                               
      INSERT INTO PF8_D(NAME,GENDER) VALUES(P_NAME,P_GENDER);  
      IF SQLCODE = 0 THEN                                      
        SET SUCCESS = '1';                                     
      SET STMT = 'CREATE OR REPLACE VARIABLE TESTLIB.OUT '  
                 CONCAT 'CHAR(1) DEFAULT ''1''';               
      EXECUTE IMMEDIATE STMT;                                  
      ELSE                                                     
       SET SUCCESS = '0';                                     
      SET STMT = 'CREATE OR REPLACE VARIABLE TESTLIB.OUT ' 
                 CONCAT 'CHAR(1) DEFAULT ''0''';              
      EXECUTE IMMEDIATE STMT;                                 
      END IF;                                                 
      END   

      In the SQL stored procedure code, a few lines are written intentionally such as we are writing create or replace SQL variables and setting their values as 1 or 0 according to SQL code value. Here, the SQL variable works as a return value for my CL program. Please note that SQL variable value is accessible to the same session in which its value is set by default during the create variable statement or SET statement.

    3. Compile the SQL stored procedure using the RUNSQLSTM command.
      RUNSQLSTM SRCFILE(TESTLIB/STOREDPROC) 
                SRCMBR(STORED33)               
                COMMIT(*NONE)                  
                DBGVIEW(*SOURCE)    
    4. Create a view in the qtemp library using the CREATE VIEW statement.
      create or replace view qtemp.rtnval   as (   
      select   testlib.out as                   
      rtn  from sysibm.sysdummy1 ) rcdfmt rtnvalf  
    5. Write a CL program calling SQL stored procedure with the output parameter.
      pgm                                                     
      dcl        var(&cmd) type(*char) len(200)               
      dcl        var(&in1) type(*char) len(20) value('name1') 
      dcl        var(&in2) type(*char) len(1)  value('M')     
      dcl        var(&sqlout) type(*char) len(1)              
      dcl        var(&LIB) type(*char) len(10) +              
                   value('TESTLIB')                        
      DCLF       FILE(rtnval)                                 
                                                              
      CHGVAR     VAR(&CMD) VALUE('create or replace variable +
                   testlib.out char(1) default '' ''')     
      call       pgm(qzdfmdb2) parm((&cmd))                   
                                                              
      CHGVAR     VAR(&CMD) VALUE('CALL +                      
                   testlib.STORED33(''' *CAT &IN1 *TCAT +  
                   ''' , ''' *CAT &IN2 *TCAT ''', ' +         
                   *TCAT &lib *TCAT '.OUT)')                  
      call       pgm(qzdfmdb2) parm((&cmd))   
                      
      RUNSQL     SQL('create or replace view qtemp/rtnval as + 
                   (select testlib.out as rtn from +        
                   sysibm.sysdummy1) rcdfmt rtnvalf') +        
                   COMMIT(*NONE)                               
      RCVF                                                     
      CHGVAR     VAR(&SQLOUT) VALUE(&RTN)    
                        
      return                                                   
      endpgm  

      Please, note that in step 1 we created a SQL variable named OUT in the TESTLIB library so that we can later create a view RTNVAL in qtemp library using that at step 4. For your knowledge as we have declared the view rtnval in my CL program so that I can populate that view with the value that was set by SQL stored procedure in SQL variable (SQL global variable).
      At the beginning of the program, we create an SQL variable out in testlib with a default blank value by passing the create or replace variable statement to the QZDFMDB2 program as a parameter in the string format.
      Then, we call the SQL stored procedure from within the CL program with input and output parameters by first building the &cmd string, and in, we pass the SQL variable instead of any CL program variable. Then we pass it to the QZDFMDB2 program as a parameter to run the SQL stored procedure. Now till this point, we have not received any return value from the SQL stored procedure call using QZDFMDB2. However, within the SQL stored procedure script, we did set the respective success/failure value in the SQL variable globally and now I am ready to receive that in my CL program which would be the actual return value by my SQL stored procedure call.
      So, I selected the global SQL variable value and output it in a view named rtnval, and after that, I just read the view using the RCVF command and evaluated the value from the view column &rtn to the cl program variable &sqlout. Therefore, &sqlout would not contain the return value or the output parameter value that should be returned by the actual SQL stored procedure call within the CL program with the output parameter

      • 1
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
    • 2024-10-09T20:50:21+05:30Replied to answer on October 9, 2024 at 8:50 pm

      Hi, I have tried calling your SQL stored procedure example case in a CL program at my end everything works fine however, there is one observation that I came across that I am mentioning here, You are using the QZDFMDB2 program to run SQL stored procedure in a CL program. So, when I am calling your CL program that internally calls QZDFMDB2 prompts to Press ENTER to end the terminal session. after executing the QZDFMDB2 call statement.

      DB20000I THE SQL COMMAND COMPLETED SUCCESSFULLY.
      Press ENTER to end terminal session.

      Can you suggest what I should change to remove the prompt in an interactive job?

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
      • Admin
        Admin
        2024-10-09T20:56:22+05:30Replied to answer on October 9, 2024 at 8:56 pm
        This answer was edited.

        Hi, Yes you can override the output to a file. Use the below code in the CL program

        1-CRTPF FILE(QTEMP/CALLOUTPUT) RCDLEN(200) + MBR(CALLOUTPUT) MAXMBRS(1) SIZE(1 0 0)
        2-OVRDBF FILE(STDOUT) TOFILE(QTEMP/CALLOUTPUT) + EXPCHK(*YES) OVRSCOPE(*CALLLVL)
        3-Call to QZDFMDB2(command string) as in your CL program
        4-DLTOVR FILE(STDOUT)
        5-DLTF FILE(QTEMP/CALLOUTPUT)
        The following highlighted modifications done to the existing CL program.

        pgm                                                      
        dcl        var(&cmd) type(*char) len(200)                
        dcl        var(&in1) type(*char) len(20) value('name1')  
        dcl        var(&in2) type(*char) len(1)  value('M')      
        dcl        var(&sqlout) type(*char) len(1)               
        dcl        var(&LIB) type(*char) len(10) +               
                     value('TESTLIB')                         
        DCLF       FILE(rtnval)                                  
        /*Start- Modified added code */
        /*Added code to create a temp file and override the temp file */
        /* to send output to temp file and ignore prompt */                                                         
        CRTPF      FILE(QTEMP/CALLOUTPUT) RCDLEN(200) +          
                     MBR(CALLOUTPUT) MAXMBRS(1) SIZE(1 0 0)      
        OVRDBF     FILE(STDOUT) TOFILE(QTEMP/CALLOUTPUT) +       
                     EXPCHK(*YES) OVRSCOPE(*CALLLVL)
        /*End- Modified added code */
        /*Added code to create a temp file and override the temp file */
        /* to send output to temp file and ignore prompt */           
                                                                 
        CHGVAR     VAR(&CMD) VALUE('create or replace variable + 
                     testlib.out char(1) default '' ''')      
        call       pgm(qzdfmdb2) parm((&cmd))                    
                                                                 
        CHGVAR     VAR(&CMD) VALUE('CALL +                       
                     testlib.STORED33(''' *CAT &IN1 *TCAT +  
                     ''' , ''' *CAT &IN2 *TCAT ''', ' +         
                     *TCAT &lib *TCAT '.OUT)')                  
        call       pgm(qzdfmdb2) parm((&cmd))                   
                                                                
        RUNSQL     SQL('create or replace view qtemp/rtnval as +
                     (select testlib.out as rtn from +       
                     sysibm.sysdummy1) rcdfmt rtnvalf') +       
                     COMMIT(*NONE)                              
        RCVF                                                    
        CHGVAR     VAR(&SQLOUT) VALUE(&RTN)                     
        /*Start- Modified added code */
        /*Added code to delete the override and delete temp file */                                                        
        DLTOVR     FILE(STDOUT) 
        DLTF       FILE(QTEMP/CALLOUTPUT)  
        /*End- Modified added code */
        /*Added code to delete the override and delete temp file */                          
        return                                                  
        endpgm  
          • 0
        • Reply
        • Share
          Share
          • Share on Facebook
          • Share on Twitter
          • Share on LinkedIn
          • Share on WhatsApp

Sidebar

Statistics

  • Questions 143
  • Answers 177
  • Comments 0
  • Popular
  • Answers
  • Admin

    Why do we use OVERLAY keyword in data structure subfields ...

    • 12 Answers
  • Admin

    How to call sql stored procedure with output parameter from ...

    • 6 Answers
  • Admin

    How to use declare global temporary table statement in RPGLE?

    • 5 Answers
  • Admin
    Admin added an answer CPF4131 is a record format level check error. This indicates… October 18, 2024 at 1:58 am
  • Admin
    Admin added an answer To open the command prompt with administrator rights you can… October 17, 2024 at 12:27 am
  • Admin
    Admin added an answer In AS400, "AS" stands for Application system. This article discusses… October 13, 2024 at 12:49 pm

Related Questions

  • Admin

    what is level check error and how to resolve level ...

    • 1 Answer
  • Admin

    What are AS400 systems. Please provide a introduction of AS400 ...

    • 1 Answer
  • Admin

    Is there any online server available for practicing on AS400 ...

    • 1 Answer
  • Admin

    What are the menus available in AS400?

    • 1 Answer
  • Admin

    What is subsystem in AS400?

    • 1 Answer

Trending Tags

.htaccess (1) as400 (123) bing-webmaster (2) control-language (12) db2 (33) ftp (8) google-adsense (1) google-search-console (3) https-redirect (1) iasp (4) ifs (22) jar (4) operations (3) php-my-admin (1) qshell (3) robots.txt (4) rpg (26) stored-procedure (3) stroed procedure (1) triggers (1) yoast (4)

Explore

  • Home
  • Categories
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags

Footer

BrainChime

BrainChime is a blog that posts question-and-answer-based format articles on diverse topics and engages in discussions by allowing people to provide answers/comments without the need to register and log in.

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • FAQs
  • Categories
  • Tags

© 2024 BrainChime. All Rights Reserved
by BrainChime.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.