• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: How To Use Global Variables In SQL Scripts

    January 23, 2017 Michael Sansoterra

    Hey, Mike:

    I’m writing SQL scripts to do some administrative work. These scripts are run in iNavigator’s RunSQL scripts utility and use the special CL: directive to execute an IBM i command. I stumbled across the CREATE VARIABLE statement and wondered if I could use an SQL variable to substitute a library name within the OS commands.

    For example, if I define something like the following in iNavigator:

    CREATE OR REPLACE VARIABLE QGPL/LIB CHAR(10) DEFAULT 'JOE';
    

    Can I then use the LIB variable in a CL: command as follows?

    CL:  DSPLIB LIB(LIB) OUTPUT(*PRINT);
    

    –Guru Reader

     

    Unfortunately, GLOBAL VARIABLES cannot be used in this manner because the IBM i command interpreter knows nothing of them. However, if you use the QSYS2.QCMDEXC procedure to execute the IBM i commands (instead of using CL:), then it’s easy to place the desired library from a global variable in an IBM i command as follows:

    BEGIN
        DECLARE @CMD VARCHAR(1024);
        SET @CMD='DSPLIB LIB(' || RTRIM(QGPL.LIB) ||') OUTPUT(*PRINT)';
        CALL QSYS2.QCMDEXC (@CMD);
    END
    

    In this case, the value in variable QGPL.LIB is inserted into the IBM i DSPLIB command string, which is then passed to the QCMDEXC stored procedure for execution. Using QCMDEXC is often preferable to CL: because it should work in any DB2 for i compatible SQL processor (including STRSQL) whereas CL: is a special “trick” supported by IBM GUI SQL tools in iAccess and Access Client Solutions.

    –Mike

     

    Michael Sansoterra is a DBA for Broadway Systems in Grand Rapids, Michigan. Send your questions or comments for Mike via the IT Jungle Contact page.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: Global Variables, SQL

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Doctor Frank Talks Power With Vision Drilling Down Into IBM’s System Group

    Leave a Reply Cancel reply

TFH Volume: 27 Issue: 2

This Issue Sponsored By

  • New Generation Software
  • T.L. Ashford
  • WorksRight Software
  • Linoma Software
  • System i Developer

Table of Contents

  • IBM i Trends, Concerns, And Observations
  • Drilling Down Into IBM’s System Group
  • Guru: How To Use Global Variables In SQL Scripts
  • Doctor Frank Talks Power With Vision
  • IBM Gives Power Systems Rebates For Linux Workloads

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Learning And Laughing With Scott Forstie And Tim Rowe
  • IBM’s CEO Says GenAI Is Great For Enterprise, But It Will Not Be AGI
  • Guru: A First Look at Bob, The IBM i Assistant That’s Closer Than You Think
  • Happy Holidays To All Of You From All Of Us
  • IBM i PTF Guide, Volume 27, Number 48
  • Bob More Than Just A Code Assistant, IBM i Chief Architect Will Says
  • Stacking Up Entry IBM i-Power11 Systems Against Windows X86 Platforms
  • IBM Brings AI-Enhanced OpenShift Container Platform To Power Systems
  • As I See It: Artificial Integrity
  • IBM i PTF Guide, Volume 27, Number 47

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle