IBM i Access Client Solutions

 View Only
  • 1.  Stored Procedure works only in ACS Run Scripts

    Posted Fri December 06, 2024 11:01 AM

    This is odd. I created this SP to automate loading spooled files to send to another system. Yes, I know there are other ways, but this should have worked.  It does work when I call the SP from Run Scripts--loading all the spooled file copies to QSPOOLSQL. When I try from the STRSQL command line or RUNSQL from the main command line, the procedure drops the old copy of QSPOOLSQL and creates a new one.  It does not load the spooled files to the members as it does using Run Scripts. Thoughts? 

    ACS: CALL MYLIB.LOAD_SPOOLED_FILES ('MYOUTQ','USERDATA','MYPRINTF','06')

    Command line: RUNSQL SQL('CALL MYLIB.LOAD_SPOOLED_FILES (''MYOUTQ'',''USERDATA'', ''MYPRINTF'',''06'')') COMMIT(*NC)

    create or replace procedure Load_Spooled_Files(    
            IN in_Out_Queue char(10) DEFAULT ' ',      
            IN in_User_Data CHAR(10) DEFAULT ' ', 
            in in_Spool_File CHAR(10) DEFAULT ' ', 
            IN in_Months char(02) DEFAULT '12'
    )                                         

    language SQL                                               
    result sets 1                                              
    SPECIFIC Load_Spooled_Files
    set option DBGVIEW = *SOURCE                               
                                                               
    -- Procedure Logic ----------------------------------------
    begin              

    declare wk_Out_Queue char(10);           
    declare wk_User_Data char(10);   
    declare wk_Spool_File char(10);   
    declare wk_Months decimal(02,0); 

    declare SpooledFileList cursor with return for        
    select user_data,spooled_file_name, file_number,'M' ||RANK() OVER(ORDER BY create_timestamp DESC) AS "Rank",
    QSYS2.QCMDEXC('CPYSPLF FILE(' || trim(spooled_file_name) ||') TOFILE(MYLIB/QSPOOLSQL) JOB(' || Trim(job_name) ||') SPLNBR('|| trim(file_number) ||') TOMBR('|| 'M' ||RANK() OVER(ORDER BY create_timestamp DESC)||')  CTLCHAR(*FCFC)  ') Pass
      From QSYS2.OUTPUT_QUEUE_ENTRIES_BASIC OQ  
     where (OUTPUT_QUEUE_NAME, user_data,spooled_file_name) = (wk_Out_Queue,wk_User_Data,wk_Spool_File) 
     and date(Create_Timestamp) >= current date - wk_Months Months
     order by create_timestamp desc  
      ; 


    SET wk_Out_Queue = upper(Trim(in_Out_Queue));
    SET wk_Months = CAST(Trim(in_Months) AS decimal(2,0)); 
    SET wk_User_Data = Trim(in_User_Data); 
    SET wk_Spool_File = Trim(in_Spool_File); 

    DROP TABLE IF EXISTS MYLIB.QSPOOLSQL; 
    call QSYS2.QCMDEXC('CRTPF FILE(MYLIB/QSPOOLSQL) RCDLEN(198) TEXT(''Copied Spooled File for '||trim(wk_User_Data) || ' '') MAXMBRS(100) SIZE(*NOMAX)'); 

     -- Run the query           
     open SpooledFileList;      
                                
    end                                     



    ------------------------------
    David Taylor
    Sr Application Developer
    Range Resources
    Fort Worth
    ------------------------------


  • 2.  RE: Stored Procedure works only in ACS Run Scripts

    Posted Fri December 06, 2024 11:25 AM

    have you tried putting it into debug to see where it fails ?  There are differences in how SQL is handled/processed when running via  STRSQL/RUNSQL  vs ACS RUN Scripts. One 'annoying' difference is that an SQL statement with a semicolon won't run via RUNSQL but runs fine elsewhere. This is a very minor difference but I wonder if your proc isn't running for a similar reason. Try putting it into DEBUG first and step thru and see where it fails. 



    ------------------------------
    Rich Malloy
    ------------------------------



  • 3.  RE: Stored Procedure works only in ACS Run Scripts

    Posted Fri December 06, 2024 01:25 PM

    @Rich Malloy, yes, I put it in DEBUG.  It does not "fail" as such. It simply does not run the QCMDEXEC part of the process.  Could be a limit in the SQL allowed from CLLE is bleeding over.  I will tear it down and create an RPGLE to do the work if the other solution does not work. 



    ------------------------------
    David Taylor
    Sr Application Developer
    Range Resources
    Fort Worth
    ------------------------------



  • 4.  RE: Stored Procedure works only in ACS Run Scripts

    Posted Fri December 06, 2024 03:29 PM

    David, I think this is your problem (item # 2)

    Restrictions when using Interactive SQL (STRSQL)

    Restrictions when using Interactive SQL (STRSQL)

    News


    Abstract

    This document provides information on the limitations of using STRSQL to run SQL statements that is provided with product 5770ST1.

    Content

    It is suggested that ACS Run SQL Script be used for running SQL statements. 
    STRSQL does not support the following:

    1.  LOB data types.  *POINTER or unreadable characters will be returned.  You can cast the data type to a CHAR when possible (limited to the max size of char field).
          Example - select cast(LOB_Field as char(100) ccsid 37 ) as LOB_FIELD_CHAR
                               FROM LIB/FILE1

    2.  SQL statements that return a result set.  




    ------------------------------
    Rich Malloy
    ------------------------------



  • 5.  RE: Stored Procedure works only in ACS Run Scripts

    Posted Sun December 08, 2024 05:27 AM

    Your stored procedure returns a result set.

    1. Result Sets are not displayed in STRSQL (STRSQL is stabilized since the Releases 5. ... )
    2. You cannot consume a stored procedure that returns a result set with CL, i.e. RUNSQL. You may use any other programming language such as RPG, SQL, C++ etc. with which you can consume result sets.


    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Birgitta Hauser - Modernization-Education-Consulting on IBM i (selfemployed)
    Kaufering
    +49 170 5269964
    ------------------------------



  • 6.  RE: Stored Procedure works only in ACS Run Scripts

    Posted Mon December 09, 2024 01:13 AM

    Dear Davis

    Since the cause of your problem is the returned result set of your SELECT statement, you have a choice of eliminating the returned result set by writing the result into a table instead. This is done by wrapping your SELECT within CREATE OR REPLACE TABLE...  AS ( SELECT ...)  or INSERT INTO ... VALUE ( SELECT ....).   You need another table to keep this result.  



    ------------------------------
    Satid S
    ------------------------------



  • 7.  RE: Stored Procedure works only in ACS Run Scripts

    Posted Tue December 10, 2024 12:59 AM
    Another workaround for you problem might be, to rewrite you SP as an user defined table function (UDTF) - which also can return data, but to a select statement, in which it is "called".

    Like: SELECT * FROM TABLE(myUdtf(parm));

    UDTFs are quite simple to create in SQL-DDL or as external UDTFs with ILE-RPG.

    If you need assistance - just send me a PM, I wrote some UDTFs in both SQL and ILE-RPG lately.

    HTH
    Daniel