Databricks SQL - Conditional Catalog query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2023 01:26 PM
Hi is there a way we can do
%sql
select * from {{ catalog }}.schema.table
Where `{{ catalog }}` is a template variable extracted/evaluated from either an environment variable, a databricks secret, or somewhere else? (note: not a widget)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 12:42 PM
You can first create a variable to extract the value you want. Then you can use python's string formatting to substitute the variable inside the query.
Let's say catalog is the variable name. Then you can perform
command = f"select * from {catalog}.schema.table"
spark.sql(command)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 12:44 PM
Sorry should have specify, i don't want to use the `spark.sql` api. I would want to use it on:
%sql
select * from {{ catalog }}.schema.table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 10:05 PM
Hi @102842
You can use query parameters to perform this - https://docs.databricks.com/sql/user/queries/query-parameters.html
You can define the catalog name as a query parameter. You should declare the catalog name parameter as a drop down list, because if we declare it as a string, it will add single quotes surrounding the value, during the substitution.

