Created
September 14, 2023 13:19
-
-
Save qpgmr-de/4e31d8033d58a39d63c69b09f54caba9 to your computer and use it in GitHub Desktop.
Retrieve the last SQL statement executed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
**free | |
//************************************************************** | |
// Control options | |
//************************************************************** | |
ctl-opt dftactgrp(*no) actgrp(*new); | |
exec sql set option closqlcsr = *endmod; | |
exec sql set option commit = *none; | |
exec sql whenever not found continue; | |
exec sql whenever sqlwarning continue; | |
//************************************************************** | |
// Main | |
//************************************************************** | |
dcl-s nCount uns(20:0) inz; | |
dcl-s sStmt varchar(10000) inz; | |
exec sql select count(*) into :nCount from (values 1, 2, 3); | |
sStmt = getLastSql(); | |
*inlr = *on; | |
return; | |
//************************************************************** | |
// Get last SQL statement of job | |
//************************************************************** | |
dcl-proc getLastSql; | |
dcl-pi *n varchar(2000000); | |
end-pi; | |
/copy qsysinc/qrpglesrc,qusec | |
/copy qsysinc/qrpglesrc,qusrjobi | |
dcl-pr retrieveJobInformation extpgm(QUSRJOBI); | |
qusi0900 char(2000000) options(*varsize); | |
datalen int(10) const; | |
datafmt char(8) const; | |
jobname char(26) const; | |
intjobid char(16) const; | |
qusec likeds(qusec); | |
end-pr; | |
dcl-ds dsInfo qualified inz; | |
qusi0900 likeds(qusi0900); | |
buffer char(2000000) pos(1); | |
end-ds; | |
dcl-ds dsError likeds(qusec) inz; | |
// init error DS | |
dsError.qusbprv = %size(dsError); | |
// now call the API | |
retrieveJobInformation(dsInfo:%size(dsInfo):'JOBI0900':'*':*blank:dsError); | |
if dsInfo.qusi0900.quslcsql > *zero; | |
return %trimr(%subst(dsInfo.buffer:dsInfo.qusi0900.qusocsql + 1:dsInfo.qusi0900.quslcsql)); | |
endif; | |
return ''; | |
end-proc; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment