Reputation: 83
In snowflake, I am trying to retrieve 2 columns in the select query, but passing one column name as an argument. Instead of returning the "VALUE" from data for the second column, it is actually returning me value for the argument I am passing from outside.
SNIPPET:
set col='NAME';
SELECT ID, $col from TABLE;
ACTUAL RESULT:
ID $col
123 NAME
EXPECTED RESULT:
ID $col
123 Mr. X
Upvotes: 0
Views: 1514
Reputation: 7369
Snowflake treats variables as strings, not as names of objects. In order to do that, you'd need to use an IDENTIFIER() function. Try something like this:
set col='NAME';
SELECT ID, IDENTIFIER($col) from TABLE;
There are some examples of this in the docs with explanation here: https://docs.snowflake.com/en/sql-reference/session-variables.html#using-variables-in-sql
Upvotes: 4