Skip to content

Instantly share code, notes, and snippets.

@AbrahamReuben
Last active September 20, 2022 14:08
Show Gist options
  • Save AbrahamReuben/f5d64be764116e4afaeb46e72c275601 to your computer and use it in GitHub Desktop.
Save AbrahamReuben/f5d64be764116e4afaeb46e72c275601 to your computer and use it in GitHub Desktop.
Do the equivalent of Find String from the Run SQL
-- Compile: RUNSQLSTM SRCFILE(MYLIB/QPSUSRC) SRCMBR(FINDSTRING) COMMIT(*NONE) MARGINS(102) DBGVIEW(*SOURCE)
-- Run: CALL MYLIB.FINDSTRING ('mycode', 'SOURCE', 'QRPGSRC,QPSUSRC,QDDSSRC', '');
CREATE OR REPLACE PROCEDURE MYLIB.FINDSTRING (
IN FIND_STRING VARCHAR(100),
IN FIND_LIBRARY VARCHAR(100),
IN FIND_SOURCE VARCHAR(100),
OUT RESPONSE VARCHAR(2048) )
LANGUAGE SQL
RESULT SETS 1
BEGIN
DECLARE STRING VARCHAR(102);
DECLARE ERRORFLAG CHAR(1);
DECLARE SQLSTRING VARCHAR(1000);
DECLARE MSGID VARCHAR(10);
DECLARE MSGTXT VARCHAR(1024);
DECLARE RESULTSET CURSOR FOR
SELECT * FROM QTEMP.RESULTS;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 MSGTXT = MESSAGE_TEXT,
MSGID = DB2_MESSAGE_ID;
SET RESPONSE = CHAR(MSGID) || ':' || TRIM(MSGTXT);
END;
DECLARE GLOBAL TEMPORARY TABLE RESULTS (
TABLE_SCHEMA VARCHAR(128),
TABLE_NAME VARCHAR(128),
TABLE_PARTITION VARCHAR(128),
SRCSEQ NUMERIC(6,2),
SRCDAT NUMERIC(6,0),
SRCDTA CHARACTER(100) )
NOT LOGGED WITH REPLACE;
SET ERRORFLAG = '';
SET RESPONSE = '';
IF FIND_STRING = '' THEN
SET ERRORFLAG = 'Y';
SET RESPONSE = 'Please provide a string to search for';
ELSEIF FIND_LIBRARY = '' THEN
SET ERRORFLAG = 'Y';
SET RESPONSE = 'Please provide the Library or comma separated Libraries ' ||
'to search for';
ELSEIF FIND_SOURCE = '' THEN
SET ERRORFLAG = 'Y';
SET RESPONSE = 'Please provide the Source File or comma separated Source ' ||
'Files to search for';
END IF;
IF ERRORFLAG = '' THEN
SET STRING = '%' || TRIM(UPPER(FIND_STRING)) || '%';
FOR V AS CUR1 CURSOR FOR
WITH SOURCEPHYSICALFILES AS (
SELECT TRIM(ELEMENT) AS SRCPF
FROM TABLE(SYSTOOLS.SPLIT(FIND_SOURCE, ','))
),
SOURCELIBRARIES AS (
SELECT TRIM(ELEMENT) AS LIB
FROM TABLE(SYSTOOLS.SPLIT(FIND_LIBRARY, ','))
)
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_PARTITION
FROM QSYS2.SYSPARTITIONSTAT
WHERE TABLE_NAME IN ( SELECT SRCPF FROM SOURCEPHYSICALFILES )
AND TABLE_SCHEMA IN ( SELECT LIB FROM SOURCELIBRARIES )
DO
SET SQLSTRING = 'CREATE ALIAS QTEMP.A1 FOR ' || TRIM(V.TABLE_SCHEMA) ||
'.' || TRIM(V.TABLE_NAME) || '(' ||
TRIM(V.TABLE_PARTITION) || ')';
EXECUTE IMMEDIATE SQLSTRING;
INSERT INTO QTEMP.RESULTS
SELECT V.TABLE_SCHEMA, V.TABLE_NAME, V.TABLE_PARTITION,
SRCSEQ, SRCDAT, SRCDTA
FROM QTEMP.A1
WHERE UPPER(SRCDTA) LIKE STRING;
DROP ALIAS QTEMP.A1;
END FOR;
OPEN RESULTSET;
END IF;
END;
@AbrahamReuben
Copy link
Author

AbrahamReuben commented Sep 20, 2022

Revision 1:

Replaced CUR1 logic

From
FOR V AS CUR1 CURSOR FOR
WITH SRCPF (MEMBER) AS
( VALUES FIND_SOURCE ),
LIBRARY (LIBRARY) as
( VALUES FIND_LIBRARY )
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_PARTITION
FROM QSYS2.SYSPARTITIONSTAT
WHERE TABLE_NAME IN
(SELECT ELEMENT
FROM SRCPF,
TABLE(SYSTOOLS.SPLIT(SRCPF.MEMBER, ',')))
AND TABLE_SCHEMA IN
(SELECT ELEMENT
FROM LIBRARY,
TABLE(SYSTOOLS.SPLIT(LIBRARY.LIBRARY, ',')))

To
FOR V_ AS CUR1_ CURSOR FOR
WITH SOURCEPHYSICALFILES AS (
SELECT TRIM(ELEMENT) AS SRCPF
FROM TABLE(SYSTOOLS.SPLIT(FIND_SOURCE, ','))
),
SOURCELIBRARIES AS (
SELECT TRIM(ELEMENT) AS LIB
FROM TABLE(SYSTOOLS.SPLIT(FIND_LIBRARY, ','))
)
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_PARTITION
FROM QSYS2.SYSPARTITIONSTAT
WHERE TABLE_NAME IN ( SELECT SRCPF FROM SOURCEPHYSICALFILES )
AND TABLE_SCHEMA IN ( SELECT LIB FROM SOURCELIBRARIES )

as the former could throw a SQL0904: Resource limit exceeded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment