selectsnowflake-cloud-data-platformsnowsql

Column bind variable in Select on Staged File #snowflake-cloud-platform


To stage and query the files the documentation shows this example query: SELECT t.$1, t.$2 FROM @mystage1 (file_format => myformat) t;

To simplify string manipulation I'd like to configure Snowflake to use anything but dollar sign as the column reference. For example, how about c instead of $?

SELECT t.c1, t.c2 FROM @mystage1 (file_format => myformat) t;

This is the link to the documentation: https://docs.snowflake.com/en/user-guide/querying-stage.html

Currently I'm forced by an application to use PowerShell to code the query to be processed by the SnowSql CLI). Making PowerShell emit this string is trivial, but then SnowSql adds its two cents and History shows the statement is processed as SELECT t., t. FROM @mystage1 (file_format => myformat) t;


Solution

  • PowerShell follows a quoting rule similar to Bash, where $VARIABLE is always resolved when found within double-quoted strings, and never resolved within single-quoted strings.

    The syntax for stage querying requires use of the $ sign which cannot be substituted for another string.

    If you are unable to use a file to pass queries to SnowSQL (via its -f [FILE] option) as a way of stepping around the shell parsing, and have to use the -q [TEXT] option, consider escaping the $ symbols, either by use of the unresolving quote marks (single quote character: ') or using the PowerShell escape character (backtick character: `):

    snowsql -q 'SELECT t.$1, t.$2 FROM @mystage1 (file_format => myformat) t;'
    
    snowsql -q "SELECT t.`$1, t.`$2 FROM @mystage1 (file_format => myformat) t;"