โ01-11-2022 06:50 AM
Hello everyone,
I'm new in Databricks SQL, and I'm comming from SQL Server.
I would like to know what's the equivalent of :
DECLARE @P_Name varchar(50) = 'BackOffice'
It's for use it like this :
CREATE DATABASE @P_Name
Thanks.
โ10-03-2022 10:54 AM
Please note that SQL Variables are supported in pure SQL. Here is an example notebook. https://github.com/rchynoweth/DemoContent/blob/main/Misc/SQL_Variables.sql
The syntax would be something like the following:
SET var.database_name = my_database ;
SELECT * FROM ${var.database_name}.my_table;
Note that the "var" can be any string i.e. it can be c.var_name instead.
โ01-11-2022 04:52 PM
Hi there, @Salah K.โ! Welcome to the community and thanks for your question. Let's give the community more time to answer your question. Thanks!
โ01-12-2022 01:26 AM
Hi,
Ok, thanks.
โ01-12-2022 04:49 AM
@Salah K.โ Databricks SQL does not support T-SQL style variable declarations. However, Databricks SQL _does_ support variables in a different way.
Try this: "
CREATE DATABASE {{ database_name }}
You will see a "widget" where you can pass in the database name
โ01-12-2022 05:31 AM
Thank you for your answer.
How can I use it like this please :
CREATE DATABASE CONCAT({{ database_name }}, 'BackOIffice')
Because it doesn't work.
โ01-12-2022 05:36 AM
@Salah K.โ that won't work because these variables are evaluated outside of SQL.
โ01-12-2022 05:52 AM
Ok.
So in Databricks SQL when can't create Stored Procedure for example ?
โ01-12-2022 06:06 AM
No, we do support UDFs (our name for sprocs). For example:
CREATE OR REPLACE FUNCTION generate_db_name(name STRING COMMENT 'Database name')
RETURNS STRING
COMMENT 'Creates a database name postfixed by BackOffice'
CONTAINS SQL DETERMINISTIC
RETURN name || 'BackOffice'
However, this fails as it appears CREATE DATABASE only accepts a string literal.
create database generate_db_name('Testing');
โ01-12-2022 06:20 AM
Ok, thank you very for your time.
โ01-19-2022 02:04 AM
Use python and build up a sql string which can be executed.
e,g
a = 'backoffice'
sql = f'select * from {a}'
print(sql)
spark.sql(sql)
โ02-14-2022 12:47 AM
Thanks
โ05-31-2022 11:00 AM
Hello,
Maybe you are looking that:
%sql
CREATE WIDGET TEXT P_Name DEFAULT 'BackOffice';
CREATE DATABASE getArgument("P_Name ")
https://docs.databricks.com/notebooks/widgets.html#widgets-in-sql
โ10-03-2022 10:54 AM
Please note that SQL Variables are supported in pure SQL. Here is an example notebook. https://github.com/rchynoweth/DemoContent/blob/main/Misc/SQL_Variables.sql
The syntax would be something like the following:
SET var.database_name = my_database ;
SELECT * FROM ${var.database_name}.my_table;
Note that the "var" can be any string i.e. it can be c.var_name instead.
โ03-22-2023 06:31 PM
This should be the accepted answer!
โ10-03-2022 03:05 PM
Awesome addition, thanks @Ryan Chynowethโ !
Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโt want to miss the chance to attend and share knowledge.
If there isnโt a group near you, start one and help create a community that brings people together.
Request a New Group