Forum Discussion
Parameterize Database Name in SparkSQL Notebook
- 2 years ago
I don't know how to pass variables into a %%sql cell.
I only know how to pass variables into spark.sql()
You could wrap your SQL code inside spark.sql()
I think you can do it something like this (I don't have Fabric opened, so I cannot verify the syntax):
spark.sql(```your
SQL
code
use a
{variable}
here
```)
ChatGPT suggested this code instead:
# Define a variable
variable = 'some_value'
# Use the variable inside spark.sql()
query = f"""
SELECT *
FROM some_table
WHERE some_column = '{variable}'
"""
result = spark.sql(query)
Hi frithjof_v
yes by database it is lakehouse.
By SparkSql I mean %%sql, i am tbe SparkSql as the Global language for the notebook
as of now everything is in same workspace.
The below screenshot shows you how i am using getting the data from the lakehosue to another lakehouse.
So we have to make the Lakehouse name Parameterized in here.
We don't to use the PySpark, the requiement is with SparkSql i.e., %%sql.
So can you provide a solution for this.
I have tried using Dataframe it worked but not when using the SparkSQL as global language
Here, for this project we have to use only SparkSql(%%sql) as the Golbal language.
I don't know how to pass variables into a %%sql cell.
I only know how to pass variables into spark.sql()
You could wrap your SQL code inside spark.sql()
I think you can do it something like this (I don't have Fabric opened, so I cannot verify the syntax):
spark.sql(```your
SQL
code
use a
{variable}
here
```)
ChatGPT suggested this code instead:
# Define a variable
variable = 'some_value'
# Use the variable inside spark.sql()
query = f"""
SELECT *
FROM some_table
WHERE some_column = '{variable}'
"""
result = spark.sql(query)
- Shanthan1182 years agoFrequent Visitor
Thank You frithjof_v
For the Response and suggestion.
when i have also checked on internet and chatgpt i too got the same solution.
So, we have created the notebook using spark.sql() it worked. - frithjof_v2 years ago
Community Champion
I've never tried, but perhaps this shows a way to pass variables into %%sql?
Disclaimer: I don't know anything about these methods. I just found them by googling briefly.