Skip to content

Instantly share code, notes, and snippets.

@NielsLiisberg
Last active January 24, 2024 12: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/d33b4697c04078c32bea5d219aa49111 to your computer and use it in GitHub Desktop.
Save NielsLiisberg/d33b4697c04078c32bea5d219aa49111 to your computer and use it in GitHub Desktop.
SQL: set environment variable
-- Set environment variable from a string value
-- you need library QSYSINC installed:
-- https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/apiref/conQSYSINC.htm
--
-- 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 2023
--
-- 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 <stdlib.h>
char env [32000];
SETENVVAR.ENVIRONMENT_VARIABLE.DAT[SETENVVAR.ENVIRONMENT_VARIABLE.LEN] =0;
SETENVVAR.ENVIRONMENT_VALUE.DAT[SETENVVAR.ENVIRONMENT_VALUE.LEN] =0;
strcpy ( env, SETENVVAR.ENVIRONMENT_VARIABLE.DAT);
strcat ( env , "=");
strcat ( env , SETENVVAR.ENVIRONMENT_VALUE.DAT);
putenv ( env) ;
}
');
create or replace procedure qusrsys.setenvvar (
environment_variable varchar(256),
environment_value varchar(32000)
)
external action
modifies sql data
deterministic
set option output=*print, commit=*none, dbgview = *source --list
main:
begin
include '/tmp/main.c';
end;
-- Usecase
call qusrsys.setenvvar ('MYENVVAR' , 'Some text');
-- using my other GETENV to look at the result
values (
qusrsys.envvar ('MYENVVAR')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment