11-15-2022 03:07 AM
I want to define a variable and use it in a query, like below:
%sql
SET database_name = "marketing";
SHOW TABLES in '${database_name}';
However, I get the following error:
ParseException:
[PARSE_SYNTAX_ERROR] Syntax error at or near ''''(line 1, pos 15)
== SQL ==
SHOW TABLES in ''
I am aware that I can use "spark.sql("query")" and python f-string. But, I would like to know how to use SQL for this purpose.
Thanks.
11-15-2022 11:37 AM
Hi @Mohammad Saber ,
SET is used for Setting Spark parameters.
I think the closest alternative to what you are trying to achive is to use widgets
https://docs.databricks.com/notebooks/widgets.html#language-sql
CREATE WIDGET TEXT database DEFAULT "customers_dev"
Then
SHOW TABLES IN "${database}"
11-15-2022 11:37 AM
Hi @Mohammad Saber ,
SET is used for Setting Spark parameters.
I think the closest alternative to what you are trying to achive is to use widgets
https://docs.databricks.com/notebooks/widgets.html#language-sql
CREATE WIDGET TEXT database DEFAULT "customers_dev"
Then
SHOW TABLES IN "${database}"
11-19-2022 02:10 AM
Hi @Mohammad Saber, We haven’t heard from you since the last response from @Pat Sienkiewicz, and I was checking back to see if his suggestions helped you.
Or else, If you have any solution, please share it with the community, as it can be helpful to others.
Also, Please don't forget to click on the "Select As Best" button whenever the information provided helps resolve your question.
09-28-2023 01:43 PM
Another option is demonstrated by this example:
%sql
SET database_name.var = marketing;
SHOW TABLES in ${database_name.var};
SET database_name.dummy= marketing;
SHOW TABLES in ${database_name.dummy};
FYI: Widget Documentation explains in the blue Note that using the widget method will not work when you Run All or run from another notebook: Databricks widgets | Databricks on AWS
09-28-2023 01:52 PM
Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections.
Click here to register and join today!
Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.