Salah K.
Salah K.

Reputation: 143

Create a Database with name from variable on Databricks (in SQL, not in Spark)

How to create a database with a name from a variable (in SQL, not in Spark) ?

I've written this :

%sql
SET myVar = CONCAT(getArgument('env'), 'BackOffice');
CREATE DATABASE IF NOT EXISTS myVar

("env" is a dropdown widgets)

But it creates me a database called "myvar".

EDIT 1 :

When I use ${myVar}, it shows me this :

enter image description here

And this :

enter image description here

Here is the link of "current SQL widgets" : https://docs.databricks.com/notebooks/widgets.html#widgets-in-sql

EDIT 2 :

When I type this, it works :

enter image description here

But not this :

enter image description here

Upvotes: 1

Views: 1983

Answers (1)

Alex Python
Alex Python

Reputation: 78

You can accomplish this by string interpolating using widgets:

CREATE DATABASE IF NOT EXISTS ${env}_BackOffice

Upvotes: 1

Related Questions