Get Sysname using SQL
The IBM i System Name is not the same thing as the SQL Local DB Server name, although it may be the same by default. You can retrieve the SQL Local DB Server name using this SQLRPGLE code snippet:
exec sql values current server into :systemName; If systemName = 'my-ibm-i-system-name'; do some stuff... endif;
* updated with code Correction from Steve Croy. Thanks!
Get Sysname using CL
The good old tried and tested technique is to write a simple Control Language Program that looks something like this:
dcl &SysName *char rtvneta sysname( &SysName )
the main drawback with this simple method is that you have another program out there to be called by any/all programs that want to find the system name.
Get Sysname using IBM API
Most RPG programmers would probably choose to call the IBM *API to Retrieve Network Attributes (QWCRNETA). This has the same affect as calling a CL to retrieve the network attributes, but of course you could also call the *API from the CLP program if you wanted to be uber-tricky 😉
// Prototype for the Rtvsysname function D RtvSysName PR 8A // Variable that will hold the system name D MYSYSTEM S 8A // simply add this line of code to get the system name C eval MYSYSTEM = RtvSysName() C eval *inlr = *on // Retrieve System Name Subprocedure P RtvSysName B Export D RtvSysName PI 8A D QWCRNETA PR ExtPgm('QWCRNETA') D RcvVar 32766A OPTIONS(*VARSIZE) D RcvVarLen 10I 0 const D NbrNetAtr 10I 0 const D AttrNames 10A const D ErrorCode 256A // Error code structure D apiError DS 256 // Receiver variable for QWCRNETA D apiData ds D Filler 32A D RtnSystem 8A // Call the *API and return the system name C callp QWCRNETA (apiData : C %size(apiData) : C 1 : C 'SYSNAME' : C apiError ) C return RtnSystem P E
and there you have it…
[thrive_headline_focus title=”STOP – SMELLY OLD RPG ALERT” orientation=”left”]
As you can see this little RPG code snippet is written in the old fashioned RPG400 flavour of RPGLE.
Old stuff is never as good as new stuff – unless it’s Whisky, Red Wine or last nights curry.
If you are a more progressive propeller head then lets see what this code would look like if I run it the UPGSRCMBR command to upgrade it to RPGLE/Free. So check out the Modernized version of this same program – in **FREE RPG code
[thrive_link color=’blue’ link=’https://www.nicklitten.com/modernize-rpg-code-get-system-name-ibm-i-iseries-and-as400′ target=’_self’ size=’medium’ align=’aligncenter’]View this code snippet in modern **FREE RPG[/thrive_link]