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
Original Message:
Sent: Wed April 17, 2024 05:17 AM
From: Christian Jorgensen
Subject: IBM i Service: Generate_Spreadsheet and Send_Email in batch
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
Original Message:
Sent: Tue April 16, 2024 04:18 PM
From: Peter Langhammer
Subject: IBM i Service: Generate_Spreadsheet and Send_Email in batch
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:
![](https://dw1.s81c.com//IMWUC/MessageImages/43db95fb0e514b3c9dc0df5cb9edc73e.png)
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:
![](https://dw1.s81c.com//IMWUC/MessageImages/f0b368b5e394475a91770cb86c1f1700.png)
Did anybody get something like this to work besides doing it interactive ?
------------------------------
Peter Langhammer
------------------------------