Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

60 Days of Data Days! Live and on-demand sessions, challenges, study groups and more! And it's all FREE!. Join now. Learn more

Reply
mrojze
Helper II
Helper II

How to dynamically generate a spark.sql using paramters?

I am new to Spark.

I am trying to insert a row into a Delta table in my lakehouse with parameters sent by a ADF pipeline.

I am having a few issues, but the main one is that I can't figure out how to insert a row into a Delta table, using a dynamic SQL.

 

mrojze_0-1694185682334.png

 

I'm not sure how to use the parameters either....

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi   @mrojze ,
To insert a row into a Delta table using dynamic SQL, you can use the spark.sql method in Scala or Python. Here's an example in Scala: 

val myTable = "myDeltaTable"
val column1 = "value1"
val column2 = "value2"
val sqlQuery = s"INSERT INTO $myTable VALUES ('$column1', '$column2')"
spark.sql(sqlQuery)

In this example, we define the name of the Delta table myTable and the values for the two columns column1 and column2. We then use string interpolation to dynamically generate an SQL query that inserts a row into the Delta table. Finally, we pass the SQL query to the spark.sql method to execute it.

 

 Here's an example in Python:

 

myTable = "myDeltaTable"
column1 = "value1"
column2 = "value2"
sqlQuery = "INSERT INTO {} VALUES ('{}', '{}')".format(myTable, column1, column2)
spark.sql(sqlQuery)


In this example, we define the name of the Delta table myTable and the values for the two columns column1 and column2. We then use placeholders {} in the SQL query and pass the parameter values as arguments to the format method. Finally, we pass the SQL query to the spark.sql method to execute it.

 

 

I have tried inserting data into my table named test_table_3 using pyspark commands. You can refer to the screenshots below.

 

 667efdd4-8075-4259-8224-ff6255d193b3.jpg

7796f8c9-3afe-4025-ab0b-404acf8b35c9.jpg

40a3670c-a5ae-46ed-8e0f-693298337583.jpg

 

Hope this helps. Please let us know if you have any further issues.
Thank you.

 

View solution in original post

2 REPLIES 2
mrojze
Helper II
Helper II

Thanks! This is so helpful.

I ended up creating a dataframe and applying a merge. Your solution works great, but I still need to learn the basics of pyspark. If I want to do a conditional update I need to learn how to do an IF statement 🙂

Because your solution doesn't work with a MERGE, does it?

 

mrojze_0-1694620406613.png

 
Anonymous
Not applicable

Hi   @mrojze ,
To insert a row into a Delta table using dynamic SQL, you can use the spark.sql method in Scala or Python. Here's an example in Scala: 

val myTable = "myDeltaTable"
val column1 = "value1"
val column2 = "value2"
val sqlQuery = s"INSERT INTO $myTable VALUES ('$column1', '$column2')"
spark.sql(sqlQuery)

In this example, we define the name of the Delta table myTable and the values for the two columns column1 and column2. We then use string interpolation to dynamically generate an SQL query that inserts a row into the Delta table. Finally, we pass the SQL query to the spark.sql method to execute it.

 

 Here's an example in Python:

 

myTable = "myDeltaTable"
column1 = "value1"
column2 = "value2"
sqlQuery = "INSERT INTO {} VALUES ('{}', '{}')".format(myTable, column1, column2)
spark.sql(sqlQuery)


In this example, we define the name of the Delta table myTable and the values for the two columns column1 and column2. We then use placeholders {} in the SQL query and pass the parameter values as arguments to the format method. Finally, we pass the SQL query to the spark.sql method to execute it.

 

 

I have tried inserting data into my table named test_table_3 using pyspark commands. You can refer to the screenshots below.

 

 667efdd4-8075-4259-8224-ff6255d193b3.jpg

7796f8c9-3afe-4025-ab0b-404acf8b35c9.jpg

40a3670c-a5ae-46ed-8e0f-693298337583.jpg

 

Hope this helps. Please let us know if you have any further issues.
Thank you.

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

June Fabric Update Carousel

Fabric Monthly Update - June 2026

Check out the June 2026 Fabric update to learn about new features.