IBM i Global

 View Only
Expand all | Collapse all

IBM i Service: Generate_Spreadsheet and Send_Email in batch

  • 1.  IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Tue April 16, 2024 04:18 PM

    I have this great sql script which works just wonderful when executing interactive in Run SQL Scripts:
    Power10 V7R4 PTF's up to date.

    --
    -- Get PTF Currency and email as Excel file
    --
    VALUES SYSTOOLS.GENERATE_SPREADSHEET(
            PATH_NAME => '/home/peter/PTF_Currency',
            SPREADSHEET_QUERY => 'WITH iLevel (iVersion, iRelease) AS (
            SELECT OS_VERSION, OS_RELEASE
                FROM sysibmadm.env_sys_info
        )
        SELECT P.*
            FROM iLevel, systools.group_ptf_currency P
            WHERE ptf_group_release = ''R'' CONCAT iVersion CONCAT iRelease CONCAT ''0''
                   AND PTF_GROUP_CURRENCY IS NOT null
            ORDER BY ptf_group_level_available - ptf_group_level_installed DESC'
    ,
            SPREADSHEET_TYPE => 'xlsx', COLUMN_HEADINGS => 'LABEL');

    -- One time setup - Make sure the user running this SQL is an SMTP user
    --call qsys2.qcmdexc('QSYS/ADDUSRSMTP  USRPRF(' concat user concat ')');

    -- Send the email with attachement
    VALUES SYSTOOLS.SEND_EMAIL(
            TO_EMAIL => 'planghammer@usinger.com', SUBJECT => 'PTF Currency: ' CONCAT (SELECT host_name
                        FROM sysibmadm.env_sys_info), BODY => 'PTF Currency on: ' CONCAT
                CURRENT TIMESTAMP, ATTACHMENT => '/home/peter/PTF_Currency.xlsx');

    Gets the PTF currency and emails as a spreadsheet, love it.

    Wanted to run that as a scheduled job.

    1st try from CLLE:

                 PGM

                 RUNSQLSTM  +
                              SRCSTMF('/SQL_Scripts/GeneralPurpose/GetPTFCur.sql+
                              ') COMMIT(*NONE) NAMING(*SYS)

                 ENDPGM

    Compiles nicely but bombs when running:

    So RUNSQLSTM doesn't like that at all, no surprise, the documentation says it has limitations.

    So the next thought was to put it into a SQLRPGLE and schedule that.
    Like this:

    **Free
    // Get PTF Currrency and email it.


    Dcl-s ReturnCode int(3);

    Exec Sql
      SET :Returncode = SYSTOOLS.GENERATE_SPREADSHEET(
            PATH_NAME => '/home/peter/PTF_Currency', SPREADSHEET_QUERY => 'WITH iLevel (iVersion, iRelease) AS
           (SELECT OS_VERSION, OS_RELEASE
               FROM sysibmadm.env_sys_info)
       SELECT P.*
       FROM iLevel, systools.group_ptf_currency P
       WHERE ptf_group_release = ''R'' CONCAT iVersion CONCAT iRelease CONCAT ''0''
       AND PTF_GROUP_CURRENCY IS NOT null
       ORDER BY ptf_group_level_available - ptf_group_level_installed DESC', SPREADSHEET_TYPE => 'xlsx',
            COLUMN_HEADINGS => 'LABEL');

    Exec Sql
      SET :ReturnCode = SYSTOOLS.SEND_EMAIL(
            TO_EMAIL => 'planghammer@usinger.com', SUBJECT => 'PTF Currency: ' CONCAT (SELECT host_name
                  FROM sysibmadm.env_sys_info), BODY => 'PTF Currency on: ' CONCAT CURRENT TIMESTAMP,
            ATTACHMENT => '/home/peter/PTF_Currency.xlsx');


    *InLr = *On;

    The compiler and the edit checker don't like the Generate_Spreadsheet at all.

    When this part gets commented out and only the Send_Email is left,
    the program compiles :-)

    But it fails when executing:

    Did anybody get something like this  to work besides doing it interactive ?



    ------------------------------
    Peter Langhammer
    ------------------------------


  • 2.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Tue April 16, 2024 08:51 PM

    Dear Peter

    I see a clear mistake in the first half of your post.  The SQL code you posted used *SQL naming by you used the parameter NAMING(*SYS) in RUNSQLSTM command which should likely be the cause of the error you encountered.  Please try NAMIMG(*SQL) to see if it solves your problem or not.



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



  • 3.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Wed April 17, 2024 10:44 AM

    Satid,
    thanks for your answer, I will have a closer look at this and give you a feedback. Thanks, Peter



    ------------------------------
    Peter Langhammer
    ------------------------------



  • 4.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Wed April 17, 2024 12:44 PM

    Satid,
    actually IBM changed that in 2020:
    "System naming convention expanded to permit (slash) and (dot) qualifiers"

    https://www.ibm.com/support/pages/system-naming-convention-expanded-permit-slash-and-dot-qualifiers

    so that didn't change the result.
    The problem was the limitations of RUNSQLSTM which has been resolved by the suggestions in the posts below.


    Thanks,
    Peter



    ------------------------------
    Peter Langhammer
    ------------------------------



  • 5.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Wed April 17, 2024 08:32 PM

    Dear Peter

    Thanks a lot for your informative response from which I now learn a new update on SQL naming support.  A good thing for being a part of this community is that you get yourself updated continually. :-) 



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



  • 6.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    IBM Champion
    Posted Wed April 17, 2024 05:18 AM

    Hi Peter.

    Your VALUES statements returns data like SELECT statements do - and RUNSQLSTM can't run such statements since it has nowhere to display the output.

    The SYSTOOLS.GENERATE_SPREADSHEET and SYSTOOLS.SEND_EMAIL both are functions and return a value 1 when no error occurred, so you can encapsulate the functions in a compound SQL statement (BEGIN-END) like this:

    --
    -- Get PTF Currency and email as Excel file
    --
    begin
    declare v_Sheet integer;
    declare v_Email integer;

    set v_Sheet =SYSTOOLS.GENERATE_SPREADSHEET(
            PATH_NAME => '/home/peter/PTF_Currency',
            SPREADSHEET_QUERY => 'WITH iLevel (iVersion, iRelease) AS (
            SELECT OS_VERSION, OS_RELEASE
                FROM sysibmadm.env_sys_info
        )
        SELECT P.*
            FROM iLevel, systools.group_ptf_currency P
            WHERE ptf_group_release = ''R'' CONCAT iVersion CONCAT iRelease CONCAT ''0''
                   AND PTF_GROUP_CURRENCY IS NOT null
            ORDER BY ptf_group_level_available - ptf_group_level_installed DESC'
    ,
            SPREADSHEET_TYPE => 'xlsx', COLUMN_HEADINGS => 'LABEL');

    -- One time setup - Make sure the user running this SQL is an SMTP user
    --call qsys2.qcmdexc('QSYS/ADDUSRSMTP  USRPRF(' concat user concat ')');

    -- Send the email with attachement
    set v_Email = SYSTOOLS.SEND_EMAIL(
            TO_EMAIL => 'planghammer@usinger.com', SUBJECT => 'PTF Currency: ' CONCAT (SELECT host_name
                        FROM sysibmadm.env_sys_info), BODY => 'PTF Currency on: ' CONCAT
                CURRENT TIMESTAMP, ATTACHMENT => '/home/peter/PTF_Currency.xlsx');

    -- Any errors?
    if v_Sheet <> 1 or v_Email <> 1 then
      call SYSTOOLS.LPRINTF( 'Error mailing PTF currency spreadsheet in job ' concat QSYS2.JOB_NAME );
      call SYSTOOLS.LPRINTF( 'Spreadsheet: ' concat v_Sheet concat ', email: ' concat v_Email );
    end if;
    end

    This will allow the statement to be run by RUNSQLSTM.



    ------------------------------
    Christian Jorgensen
    IT System Administrator
    Network of Music Partners A/S
    ------------------------------



  • 7.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Wed April 17, 2024 10:47 AM

    Christian, thanks so much, that looks like the perfect solution, that will overcome the limitation of the RUNSQLSTM. I'll give it a try later today and give feedback.
    Thank you, Peter



    ------------------------------
    Peter Langhammer
    ------------------------------



  • 8.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Wed April 17, 2024 10:36 AM

    Hi!

    Instead of VALUES, try this:

    BEGIN                                                                                                                
        DECLARE Status INTEGER DEFAULT 0;                                                                                
        SET Status = SYSTOOLS.GENERATE_SPREADSHEET(PATH_NAME => '/home/wmbeprn/Precisely_Mimix_Keys',                    
                    SPREADSHEET_QUERY => 'SELECT * from qgpl.mimlic',                                                    
                    LIBRARY_NAME => NULL, FILE_NAME => NULL, SPREADSHEET_TYPE => 'xlsx', COLUMN_HEADINGS => 'COLUMN');   
    END;                                                                                                                 

    /Bengt



    ------------------------------
    Bengt Persson
    ------------------------------



  • 9.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Wed April 17, 2024 10:48 AM

    Bengt, thank you very much! That will help, I'll test it later today and provide feedback. Peter



    ------------------------------
    Peter Langhammer
    ------------------------------



  • 10.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Wed April 17, 2024 12:48 PM

    If you want to use RPG, I think you should "continuate" your multi line string with "+"

    'select xyx +

    xxx +

    fasdfasd'

    And for general readability you can then specify host variables

    PATH_NAME => :ifsPath

    etc.

    Anyway PTF_GROUP_CURRENCY  should already return the status of the current partition if I'm not mistaken, without joining it with the WITH table.



    ------------------------------
    --ft
    ------------------------------



  • 11.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    Posted Wed April 17, 2024 12:57 PM

    Thanks to everybody coming up so quick with help to solve the problem, that is why it is so great to be a member of the IBM i community.
    Declaring a variable for the receiving of the return code did the job, RUNSQLSTM is no longer complaining.

    I ran into another problem though, a problem with sql code -332 character conversion from CCSID 65535 to 1200 not valid.

    Turns out on our box system value  QCCSID was never adjusted from 65535 to a correct 37 for our region,
    that has been adjusted now.

    Means we can now create spreadsheets in batch and mail them out on a scheduled job.
    The PTF currency was more or less a POC, there are way more value generating cases where that makes sense.

    Thanks for a quick solution!
    Peter



    ------------------------------
    Peter Langhammer
    ------------------------------



  • 12.  RE: IBM i Service: Generate_Spreadsheet and Send_Email in batch

    IBM Champion
    Posted Thu April 18, 2024 07:05 AM

    Glad to see you've changed your QCCSID from 65535.

    I swear IBM picks this one to force you to make a decision.  Many of us have changed our main production boxes midday and never looked back.

    Too many people will not out of fear of change.  "Better the devil you know than the devil you don't."  I wonder how many of these, if they were on an appropriate one for their region, would change to 65535?



    ------------------------------
    Robert Berendt IBMChampion
    ------------------------------