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 Shanthan118
If you want to store the database name in a notebook then reference it by another notebook, you can use Microsoft Spark Utilities (MSSparkUtils) in Fabric. Microsoft Spark Utilities (MSSparkUtils) is a built-in package and available in PySpark (Python) Scala, SparkR notebooks, and Fabric pipelines. It is not available in SparkSQL. So if you want to use Notebook in SparkSQL only, I'm afraid it is a big challenge currently.
Microsoft Spark Utilities (MSSparkUtils) for Fabric - Microsoft Fabric | Microsoft Learn
Another alternative is to use the pipeline to trigger a notebook activity and edit the parameter values in the pipeline, which is suggested by SachinNandanwar. You could refer to Integrate a notebook section from Develop, execute, and manage notebooks - Microsoft Fabric | Microsoft Learn
Best Regards,
Jing
If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!