Skip to content

Instantly share code, notes, and snippets.

@NielsLiisberg
Created June 22, 2022 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NielsLiisberg/badbbdac2f58c3da4c73c38f99b74a91 to your computer and use it in GitHub Desktop.
Save NielsLiisberg/badbbdac2f58c3da4c73c38f99b74a91 to your computer and use it in GitHub Desktop.
SQL Retrieve system name by MI call
-- Retrieve the system name by call to MI
-- This also showcase how to integrate the C code directly into your UDTF
--
-- Simply paste this gist into ACS SQL and step through the example.
--
-- It is a cool example how far you can go with SQL: Have fun 😀
-- (C) Niels Liisberg 2022
--
-- This gist is distributed on an "as is" basis, without warranties
-- or conditions of any kind, either express or implied.
----------------------------------------------------------------------------------------------
call qsys2.ifs_write(
path_name => '/tmp/main.c' ,
file_ccsid => 1208,
overwrite => 'REPLACE',
line =>'
{
#include <QSYSINC/MIH/MATMATR>
_MMTR_Template_T template;
short attr = _MMTR_APPN;
template.Options.Template_Size = sizeof(_MMTR_Template_T);
_MATMATR1 (&template , &attr );
memcpy (
MAIN.OUTPUT,
template.Options.Data.APPN.Sys_Name ,
template.Options.Data.APPN.Name_Len
);
}
');
create or replace function qusrsys.system_name ()
returns varchar (10)
set option output=*print, commit=*none, dbgview = *source --list
main:begin
declare output char(10) for bit data default '';
include '/tmp/main.c';
return rtrim(output);
end;
-- unit test
values (
qusrsys.system_name ()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment