Skip to content

Instantly share code, notes, and snippets.

@BirgittaHauser
Last active October 2, 2023 07:31
Show Gist options
  • Save BirgittaHauser/8a95ec08f00f99f708ac42de0e5d9332 to your computer and use it in GitHub Desktop.
Save BirgittaHauser/8a95ec08f00f99f708ac42de0e5d9332 to your computer and use it in GitHub Desktop.
Here is a function whith wich you can read your source files directly with SQL:
-- 1.1. Parameters:
-- ParSrcMbr Source Member Name
-- ParSrcFile Source File
-- ParSrcLib Source Library
-- ---------------------------------------------------------------*
Create Or Replace Function YourSchema.PH_SrcMbr
(ParSrcMbr VarChar(10) ,
ParSrcFile VarChar(10) ,
ParSrcLib VarChar(10))
Returns Table (SrcSeq Dec(6, 2) ,
SrcDat Dec(6, 0) ,
SrcDta Char(132))
Language SQL
Specific PH_SrcMbr
Not Deterministic
Modifies SQL Data
Called On NULL Input
Disallow Parallel
Not Fenced
Set Option DBGVIEW = *SOURCE ,
DATFMT = *ISO ,
TIMFMT = *ISO
Begin
Declare LocSQLCmd VarChar(1024) Default '';
Declare Continue Handler for SQLState '42704' Begin End;
Declare Continue Handler for SQLException
Begin
Declare ErrText VarChar(128) Not Null Default '';
Get Diagnostics Condition 1 ErrText = MESSAGE_TEXT;
Signal SQLState 'PH001' Set MESSAGE_TEXT = ErrText;
End;
Set (ParSrcMbr,
ParSrcFile,
ParSrcLib) = (Upper(Trim(ParSrcMbr)),
Upper(Trim(ParSrcFile)),
Upper(Trim(ParSrcLib)));
Drop Alias QTEMP.TMPSRCMBR;
Set LocSQLCmd = 'CREATE OR REPLACE ALIAS QTEMP.TMPSRCMBR FOR ' concat
Trim(ParSrcLib) concat '.' concat
Trim(ParSrcFile) concat '(' concat
Trim(ParSrcMbr) concat ')';
Execute Immediate LocSQLCmd;
Return Select SrcSeq, SrcDat, SrcDta
From QTEMP.TMPSRCMBR;
End ;
Label on Specific Function YourSchema.PH_SrcMbr
is 'Read Source Physical File Member';
Comment on Specific Function YourSchema.PH_SrcMbr
is 'Read Source Physical File Member';
@mk1tools
Copy link

mk1tools commented Oct 2, 2023

Hi Birgitta, your function is very useful.
I suggest a few improvements:

  1. return SrcDta Char(240) instead of 132 so you can read large source physical file

  2. enclose the member name in double quotes so you can read member with name as "MYMBR.BAK" or similar.

set LocSQLCmd = 'CREATE OR REPLACE ALIAS QTEMP.TMPSRCMBR FOR ' concat
trim(SOURCE_LIBRARY) concat '.' concat
trim(SOURCE_FILE) concat '("' concat
trim(SOURCE_MEMBER) concat '")';
  1. filter for empty lines of code
return select SRCSEQ, SRCDAT, rtrim(SRCDTA)
from QTEMP.TMPSRCMBR
where SRCDTA <> ' ';

The reason is a trick. Sometimes I found display file member with the SRCDTA field filled by invalid blanks and the SRCDTA empty.

  1. Only for documentation
comment on parameter function YourSchema.PH_SrcMbr
(SOURCE_MEMBER is 'Member name',
SOURCE_FILE is 'File name',
SOURCE_LIBRARY is 'Library name');

Thanks a lot. Good job.
Marco

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