abc 777
abc 777

Reputation: 1

snowflake create database with variable

I am trying to create a database using variable in snowflake

set var='mytestdb';
create database  IF NOT EXISTS $var;

Is this possible? The above query is giving me error.

Upvotes: 0

Views: 808

Answers (1)

Sergiu
Sergiu

Reputation: 4608

You need to use identifier:

set my_var = 'test_database';
select $my_var;
create database IF NOT EXISTS identifier($my_var);

Result:

1
Statement executed successfully.
1
test_database
1
TEST_DATABASE already exists, statement succeeded.

For more information:

https://docs.snowflake.com/en/sql-reference/session-variables.html

Variables can also contain identifier names, such as table names. To use a variable as an identifier, you must wrap it inside IDENTIFIER(), e.g. IDENTIFIER($MY_VARIABLE). Some examples are below:

Upvotes: 4

Related Questions