I'm running into a bit of a snag using Netezza's nzsql
tool when it comes to using internal variables. The documentation mentions how to set those variables, \set varname value
but doesn't make clear how to use them in any meaningful way.
I've worked out that they can be used within queries, but so far I can only use them as identifiers, not strings.
For example, this works fine:
DB.INST1(INST1)=> \set COLNAME 'table_name'
DB.INST1(INST1)=> SELECT :COLNAME FROM _v_sys_columns LIMIT 1;
TABLE_NAME
----------------------------
_V_TABLE_ONLY_STORAGE_STAT
(1 row)
DB.INST1(INST1)=> \p
SELECT table_name FROM _v_sys_columns LIMIT 1;
But this doesn't:
DB.INST1(INST1)=> \set TABLE_NAME 'table_to_search_for'
DB.INST1(INST1)=> SELECT owner, createdate FROM _v_table WHERE tablename = :TABLE_NAME;
ERROR: Attribute 'TABLE_TO_SEARCH_FOR' not found
DB.INST1(INST1)=> \p
SELECT owner, createdate FROM _v_table WHERE tablename = table_to_search_for;
And neither does this (it uses :TABLE_NAME
as a text literal, not its contents):
DB.INST1(INST1)=> \set TABLE_NAME 'table_to_search_for'
DB.INST1(INST1)=> SELECT owner, createdate FROM _v_table WHERE tablename = ':TABLE_NAME';
OWNER | CREATEDATE
-------+------------
(0 rows)
DB.INST1(INST1)=> \p
SELECT owner, createdate FROM _v_table WHERE tablename = ':TABLE_NAME';
Is there any way to accomplish what I'm hoping to do? The result I'm hoping to achieve is having nzsql run the following query:
SELECT owner, createdate FROM _v_table WHERE tablename = 'table_to_search_for';