0

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.

1
  • Hi @Marcel, the code is as above. just set a variable and use the variable name while creating the database. Commented Oct 22, 2020 at 10:44

1 Answer 1

4

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.