Forum Discussion

Shanthan118's avatar
Shanthan118
Frequent Visitor
2 years ago
Solved

Parameterize Database Name in SparkSQL Notebook

Hi    I am using Notebook in SparkSql in Fabric   i have created the Notebook to load data from a Lakehouse to the other lakehouse using this SparkSQL. Now that i created this notebook we have us...
  • frithjof_v's avatar
    frithjof_v
    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)