Re: using RUNSQLSTM for executing a VALUES sql statement
David,
As others have mentioned, there are restrictions on SQL statements in CL. However, I wanted to share an alternative approach we've successfully implemented that creates Excel spreadsheets directly from CL without needing GENERATE_SPREADSHEET or stored procedures.
Our Solution: Using ACS Bundle JAR with QSHONI/QSHEXEC (open source library which makes using QSH within CL much easier)
https://github.com/richardschoen/QshOni
Interestingly, this is actually what the `SYSTOOLS.GENERATE_SPREADSHEET` SQL function calls under the covers. By calling the `acsbundle.jar` file directly using the `cldownload` plugin, you can bypass the SQL statement restrictions in CL and have more control over the process.
Basic Implementation:
(Note: I've adapted this example to match your specific file names and paths from your original post)
/* Add QSHONI to library list */
ADDLIBLE LIB(QSHONI) POSITION(*LAST)
MONMSG MSGID(CPF2103)
/* Define variables */
DCL VAR(&CLIENTFILE) TYPE(*CHAR) LEN(256)
DCL VAR(&HOSTFILE) TYPE(*CHAR) LEN(256)
DCL VAR(&QSHCMD) TYPE(*CHAR) LEN(5000)
/* Set output file path and source DB2 file */
CHGVAR VAR(&CLIENTFILE) VALUE('/clientfile=/QOpenSys/ExportedFiles/CitySuiteGL/glupload/GLTOHARF.xlsx')
CHGVAR VAR(&HOSTFILE) VALUE('/hostfile=COALIBF/GLTOHARF')
/* Build the Java command */
CHGVAR VAR(&QSHCMD) VALUE('/QIBM/ProdData/Java400/bin/java -jar +
/QIBM/proddata/Access/ACS/Base/acsbundle.jar +
/plugin=cldownload /system=localhost +
/adjustcolumns /colheadings=1 /usecollabels ' +
*CAT &CLIENTFILE *BCAT &HOSTFILE)
/* Execute the command */
QSHONI/QSHEXEC CMDLINE(&QSHCMD) LOGSTDOUT(*YES) +
PRTSTDOUT(*YES) PRTUSRDTA(GLTOHARF) +
PRTTXT('Create Excel Spreadsheet')
/* Error handling */
MONMSG MSGID(CPF0000 MCH0000 QSH0006 QSH0007) EXEC(DO)
SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG) +
MSGDTA('Spreadsheet creation failed') +
TOPGMQ(*PRV (* *NONE *NONE)) MSGTYPE(*ESCAPE)
ENDDO
Key Parameters:
- `/plugin=cldownload` - Uses the client download plugin
- `/system=localhost` - Connects to local system
- `/adjustcolumns` - Auto-adjusts column widths
- `/colheadings=1` - Uses first row as headers
- `/usecollabels` - Uses DDS column labels
For Multi-Sheet Workbooks:
To add additional sheets, use `/sheet=2` and `/update` parameters:
CHGVAR VAR(&HOSTFILE) VALUE('/hostfile=COALIBF/SECONDFILE')
CHGVAR VAR(&QSHCMD) VALUE('/QIBM/ProdData/Java400/bin/java -jar +
/QIBM/proddata/Access/ACS/Base/acsbundle.jar +
/plugin=cldownload /system=localhost +
/adjustcolumns /colheadings=1 /usecollabels +
/sheet=2 /update ' *CAT &CLIENTFILE *BCAT &HOSTFILE)
QSHONI/QSHEXEC CMDLINE(&QSHCMD) LOGSTDOUT(*YES) +
PRTSTDOUT(*YES) PRTUSRDTA(SECONDFILE) +
PRTTXT('Add Sheet 2')
Advantages:
- Works entirely in CL - no stored procedures needed
- Bypasses SQL statement restrictions in RUNSQLSTM
- Uses the same underlying technology as GENERATE_SPREADSHEET
- Creates native .xlsx files with proper formatting
- Supports multiple sheets in one workbook
- Column headers and labels preserved
- More control over the export process
This approach has worked well for us as an alternative to GENERATE_SPREADSHEET when working in CL programs, giving you direct access to the same ACS functionality without the SQL limitations.
Hope this helps!