Forum Discussion
synapsesql
- 6 months ago
Hi woldea ,
This is a very common question in Microsoft Fabric notebooks, and the short answer is:You cannot directly access a Python variable from a SQL cell.Python and SQL cells run in different execution engines.
However, there are supported patterns to achieve what you want, including UPSERT/MERGE into a Warehouse.
The most recommended one is Write DataFrame → Warehouse (UPSERT supported)If your Python variable can be represented as a DataFrame, this is the best practice.
Step 1: Python – prepare data
from pyspark.sql import Row
data = [
Row(id=1, name="Alice", amount=100),
Row(id=2, name="Bob", amount=200)
]df = spark.createDataFrame(data)
Step 2: Write to Warehouse staging table
df.write \
.format("synapsesql") \
.mode("overwrite") \
.save("WarehouseName.dbo.stg_mydata")
Step 3: SQL cell – MERGE (UPSERT)MERGE dbo.target_table AS tgt
USING dbo.stg_mydata AS src
ON tgt.id = src.id
WHEN MATCHED THEN
UPDATE SET
tgt.name = src.name,
tgt.amount = src.amount
WHEN NOT MATCHED THEN
INSERT (id, name, amount)
VALUES (src.id, src.name, src.amount);
This is the Fabric-supported UPSERT pattern.If this post helps, then please appreciate giving a Kudos or accepting as a Solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot! - 6 months ago
Hi woldea
In a PySpark cell try this first
# Example Python variables target_date = "2026-01-01" batch_id = 42 status = "Complete" spark.conf.set("p_date", target_date) spark.conf.set("p_batch_id", str(batch_id)) spark.conf.set("p_status", status)In the second cell, try this
%%sql SELECT to_date('${p_date}', 'yyyy-MM-dd') AS LoadDate, CAST('${p_batch_id}' AS INT) AS BatchId, '${p_status}' AS Status;This will allow you to access a Python variable in one cell from another SQL cell in the same notebook.
Hope this helps - please appreciate by leaving a Kudos or accepting as a Solution!
- 6 months ago
Thank you deborshi_nag,
Your strategy and example was helpful and I was able to accomplish my goal.
Hi woldea ,
This is a very common question in Microsoft Fabric notebooks, and the short answer is:
You cannot directly access a Python variable from a SQL cell.Python and SQL cells run in different execution engines.
However, there are supported patterns to achieve what you want, including UPSERT/MERGE into a Warehouse.
The most recommended one is Write DataFrame → Warehouse (UPSERT supported)
If your Python variable can be represented as a DataFrame, this is the best practice.
Step 1: Python – prepare data
from pyspark.sql import Row
data = [
Row(id=1, name="Alice", amount=100),
Row(id=2, name="Bob", amount=200)
]
df = spark.createDataFrame(data)
Step 2: Write to Warehouse staging table
df.write \
.format("synapsesql") \
.mode("overwrite") \
.save("WarehouseName.dbo.stg_mydata")
Step 3: SQL cell – MERGE (UPSERT)
MERGE dbo.target_table AS tgt
USING dbo.stg_mydata AS src
ON tgt.id = src.id
WHEN MATCHED THEN
UPDATE SET
tgt.name = src.name,
tgt.amount = src.amount
WHEN NOT MATCHED THEN
INSERT (id, name, amount)
VALUES (src.id, src.name, src.amount);
This is the Fabric-supported UPSERT pattern.
If this post helps, then please appreciate giving a Kudos or accepting as a Solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!