How do I create and use session variables in Netezza
nzsql
?How can I use session variables as part of strings?
- Can I concatenate session variables with strings?
- Can I embed session variables in strings?
How can I use them as part of table names or column names?
相关问题
- Drop if exists in netezza
- Netezza date function for current date - 16 days
- How do i convert date in Netezza to yyyymmdd from
- Execute SQL file with multiple statements separate
- netezza nzload similar function to isnumeric?
相关文章
- How do i convert date in Netezza to yyyymmdd from
- Execute SQL file with multiple statements separate
- netezza nzload similar function to isnumeric?
- Field Aliasing in queries, nzsql
- Netezza公司系统目录表constains外部表中的重复记录(Netezza system ca
- FOR循环Netezza公司的问题(FOR loop Netezza issue)
- How to find the parent record using self join in N
- Find column Value by dividing with sum of a column
Basic Variable Usage
The documentation for session variables in Netezza
nzsql
is somewhat lacking.It states that in order to set a variable within a script or at the
nzsql
prompt, you use\set
.You can also specify the variable at the command line. This is useful for passing variables into scripts.
Within your session or script, you can access the value of that variable as
:var
Advanced Variable Usage
The
\set
command also has undocumented capabilities that add greater flexibility.In reality,
\set
takes all values passed to it and concatenates them together.The command also supports quoting in a manner similar to shell scripts which allows you to include whitespace within your variables.
Be careful though, quoted and unquoted strings will still be concatenated with each other.
Double quoting also works to preserves whitespace. However, the double quotes will still be preserved in the variable.
Of course, the different types of quoting can be mixed:
Single Quotes
Properly embedding single quotes in a
\set
command can be difficult.Incidentally, because double quotes are always preserved, they rarely present a problem.
Single quotes within an unquoted word will be preserved.
Single quotes within a quoted word may result in issues.
Single quotes within a doubly quoted value are preserved.
Single quotes on their own need to be quoted and escaped.
When in doubt: single quote the phrase and escape all remaining single quotes with backslashes.
Variables In Identifiers
Occasionally you will need to use a variable as part of an identifier (i.e. a column or table name).
Consider the following example table and variable:
In this example, you want to select
bar_column
using your variable:foo
(which containsbar
) and the text_column
.The following will not work:
The above example fails because
nzsql
cannot determine where the variable name ends (:foo
) and the remaining column (_column
) name begins.To fix this, you need to make a new variable with
\set
by concatenating the value of:foo
and the rest of the column name:If the variable contains the end of the identifier you wish to use, no intermediate variables need to be created.
In that specific case,
nzsql
will properly expand the variable (e.g.column_:foo
->column_bar
).Stringification
Sometimes you will need to use the contents of a variable as a string.
Consider the following example table and variable:
If you simply quote the variable within the statement then it will be treated as literal text.
If you leave the variable unquoted then it will be used as an identifier.
To fix this, you need to use
\set
and your knowledge of quoting to create a usable variable.You can accomplish this by making a new variable by combining a single quote (properly escaped!), the variable's contents, and another single quote.
If your variable needs to be used inside of a string, it may be easier to stringify your variable and use regular string concatenation.