IBM Support

Passing SQL variables using nzsql

Question & Answer


Question

How do I pass variables using nzsql

Answer

Several ways to pass variables to nzsql ....

1) Using shell script and then passing variable as unix shell variable.
shellscript.sh <TABLENAME>
in shellscript.sh....
nzsql -c "select * from $1"

2) Setting variable within nzsql.
TESTDB.ADMIN(ADMIN)=> \set tvar '\'BLAH\''
TESTDB.ADMIN(ADMIN)=> select :tvar col_alias;
 COL_ALIAS
-----------
 BLAH
(1 row)

3) Passing variable at nzsql.
[nz@nz80409-h2 ~]$ nzsql -v "tbl=backup_tmp"

TESTDB.ADMIN(ADMIN)=> select * from :tbl;
 DBNAME |      STARTTIME      |     LASTUPDATE
--------+---------------------+---------------------
        | 2014-12-23 02:43:16 | 2014-12-23 02:43:24

[{"Product":{"code":"SSULQD","label":"IBM PureData System"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"IBM Netezza Analytics","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"1.0.0","Edition":"All Editions","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Document Information

Modified date:
17 October 2019

UID

swg21984692