SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.FIND_STRING_PDM ( 
	INSTRING VARCHAR(100) , 
	INLIB CHAR(10) , 
	INFILE CHAR(10) DEFAULT  NULL  , 
	INMBR CHAR(10) DEFAULT  NULL  ) 
	RETURNS TABLE ( 
	SOURCE_LIBRARY VARCHAR(10) , 
	SOURCE_FILE VARCHAR(10) , 
	SOURCE_MEMBER VARCHAR(10) , 
	SOURCE_DESCRIPTION VARCHAR(50) , 
	SOURCE_TYPE VARCHAR(10) , 
	SOURCE_SEQUENCE INTEGER , 
	SOURCE_DATE INTEGER , 
	SOURCE_DATA VARCHAR(120) , 
	SOURCE_CREATED DATE , 
	LAST_CHANGED DATE , 
	LAST_SAVED DATE )   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.FNDSTRPDM 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	CONCURRENT ACCESS RESOLUTION DEFAULT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *CS , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SQLCURRULE = *STD , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE EOF INT NOT NULL DEFAULT 0 ; 
DECLARE SRCLIB CHAR ( 10 ) DEFAULT '' ; 
DECLARE SRCFIL CHAR ( 10 ) DEFAULT ' ' ; 
DECLARE SRCMBR CHAR ( 10 ) DEFAULT ' ' ; 
DECLARE SRCDESC VARCHAR ( 50 ) ; 
DECLARE SRCTYPE VARCHAR ( 10 ) DEFAULT ' ' ; 
DECLARE SRCCRT TIMESTAMP ; 
DECLARE SRCCHG TIMESTAMP ; 
DECLARE SRCSAV TIMESTAMP ; 
DECLARE SRCDAT INT DEFAULT 0 ; 
DECLARE SRCDTA CHAR ( 112 ) DEFAULT ' ' ; 
DECLARE SRCSEQ INT DEFAULT 0 ; 
DECLARE ALIASCMD VARCHAR ( 200 ) DEFAULT ' ' ; 
DECLARE SF2 CURSOR FOR 
SELECT SRCSEQ , SRCDAT , SRCDTA 
FROM QTEMP . SRCFILE2 
WHERE REGEXP_LIKE ( CAST ( UPPER ( SRCDTA ) AS VARCHAR ( 112 ) CCSID 37 ) , 
UPPER ( INSTRING ) ) ; 
DECLARE SF CURSOR FOR 
SELECT SYSTEM_TABLE_SCHEMA , SYSTEM_TABLE_NAME , SYSTEM_TABLE_MEMBER , 
CAST ( PARTITION_TEXT AS VARCHAR ( 50 ) CCSID 37 ) , SOURCE_TYPE , 
CREATE_TIMESTAMP , LAST_SOURCE_UPDATE_TIMESTAMP , 
LAST_SAVE_TIMESTAMP 
FROM SYSPARTITIONSTAT A 
WHERE SYSTEM_TABLE_NAME = COALESCE ( INFILE , SYSTEM_TABLE_NAME ) 
AND SYSTEM_TABLE_SCHEMA = INLIB 
AND LOCATE ( '.' , SYSTEM_TABLE_MEMBER ) = 0 
AND SYSTEM_TABLE_MEMBER = COALESCE ( 
INMBR , SYSTEM_TABLE_MEMBER ) 
AND REGEXP_LIKE ( UPPER ( 
GET_CLOB_FROM_FILE ( 
( TRIM ( SYSTEM_TABLE_SCHEMA ) || '/' || 
TRIM ( SYSTEM_TABLE_NAME ) || '(' || 
TRIM ( SYSTEM_TABLE_MEMBER ) || ')' ) ) ) , UPPER ( 
INSTRING ) ) ; 
DECLARE CONTINUE HANDLER FOR NOT FOUND SET EOF = 1 ; 
OPEN SF ; 
SF : LOOP 
FETCH NEXT FROM SF 
INTO SRCLIB , SRCFIL , SRCMBR , SRCDESC , SRCTYPE , SRCCRT , SRCCHG , 
SRCSAV ; 
IF EOF = 1 THEN 
LEAVE SF ; 
END IF ; 
SET ALIASCMD = 'Create Or Replace Alias Qtemp.SrcFile2 For ' || 
SRCLIB || '/' || SRCFIL || '(' || SRCMBR || ')' ; 
EXECUTE IMMEDIATE ALIASCMD ; 
OPEN SF2 ; 
SF2 : LOOP 
FETCH NEXT FROM SF2 
INTO SRCSEQ , SRCDAT , SRCDTA ; 
IF EOF = 1 THEN 
LEAVE SF2 ; 
END IF ; 
PIPE ( 
SRCLIB , 
SRCFIL , SRCMBR , SRCDESC , SRCTYPE , SRCSEQ , SRCDAT , SRCDTA , 
DATE ( SRCCRT ) , DATE ( SRCCHG ) , DATE ( SRCSAV ) ) ; 
END LOOP ; 
CLOSE SF2 ; 
SET EOF = 0 ; 
END LOOP ; 
CLOSE SF ; 
RETURN ; 
END  ; 

COMMENT ON PARAMETER SPECIFIC FUNCTION CISTOOLS.FNDSTRPDM 
( INSTRING IS 'Input Search String' , 
	INLIB IS 'Input Library' , 
	INFILE IS 'Input File' , 
	INMBR IS 'Input Member' ) ; 

LABEL ON SPECIFIC FUNCTION CISTOOLS.FNDSTRPDM 
	IS 'Find String Pdm' ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.FNDSTRPDM 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.FNDSTRPDM 
TO PUBLIC ; 

