Search code examples
snowflake-cloud-data-platformsnowsql

Assign snowsql select query output to an unix variable


how do we assign SNOWSQL select query output to an unix variable, i am using below shellscript and trying to assign the output of select statement to variable var2. but somehow it is not working.

#!/bin/ksh set var2=(snowsql -c newConnection -o log_level=DEBUG -o log_file=~/snowsql_sso_debug.log -r DEV_ACCT_ROLE -w LOAD_WH -d DEV_DB -s CTL_DB -q "select STG_TBL_NAME from CTL_DB.PROC_CTRL WHERE STG_TBL_NAME='TEAM'")

appriciate your quick response. Thanks


Solution

  • I can get the result using $(), but you should also use some additional options to suppresses header, timing, and the startup and exit messages.

    https://docs.snowflake.com/en/user-guide/snowsql-config.html#snowsql-configuration-options-reference

    VAR2=$(snowsql -c myconnection -q "SELECT max(v) FROM Z" -o friendly=False -o header=False -o output_format=plain -o timing=False)
    
    echo $VAR2
    20200210