The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
I have run the Stored Procedure directly inside SQL Server Management Studio, and there is no error:
EXEC dbo.Upsert_Dim_Channel 'temp_Dim_Channel'
However, I need to run the same Stored Procedure inside Fabric with PySpark as below:
I have the connection as below:
Solved! Go to Solution.
Thank you for reaching out to the Microsoft Fabric Community Forum.
The error Incorrect syntax near the keyword 'EXEC' occurs because PySpark’s JDBC connector does not directly support executing stored procedures using the EXEC or EXECUTE statement in the query option.
The spark.read.jdbc method’s query option is designed for SQL queries that return a result set. When you pass EXEC dbo.Upsert_Dim_Channel 'temp_Dim_Channel', Spark wraps it in a subquery like SELECT * FROM (EXEC ...) internally, which is invalid SQL syntax for SQL Server, leading to the Incorrect syntax near 'EXEC' error.
The stored procedure dbo.Upsert_Dim_Channel works well in SQL Server Management Studio (SSMS) because SSMS understands the EXEC syntax. However, in PySpark, the JDBC driver expects a tabular result, which can cause syntax errors for non-query statements like EXEC. Also, the stored procedure might not return a result set, whicPySpark’s spark.read.jdbc expects by default.
To execute the stored procedure dbo.Upsert_Dim_Channel in Microsoft Fabric using PySpark, you need to bypass the spark.read.jdbc limitation by using the JDBC driver’s native Java SQL capabilities (via java.sql.DriverManager) to execute the stored procedure directly.
If this response resolves your query, kindly mark it as Accepted Solution to help other community members. A Kudos is also appreciated if you found the response helpful.
Thank You!
Thank you very much v-karpurapud. Your solution of using java.sql.DriverManager works exactly to what I need. Now the Stored Procedure is able to run. I provide the code below for whoever may find this useful:
Thank you for reaching out to the Microsoft Fabric Community Forum.
The error Incorrect syntax near the keyword 'EXEC' occurs because PySpark’s JDBC connector does not directly support executing stored procedures using the EXEC or EXECUTE statement in the query option.
The spark.read.jdbc method’s query option is designed for SQL queries that return a result set. When you pass EXEC dbo.Upsert_Dim_Channel 'temp_Dim_Channel', Spark wraps it in a subquery like SELECT * FROM (EXEC ...) internally, which is invalid SQL syntax for SQL Server, leading to the Incorrect syntax near 'EXEC' error.
The stored procedure dbo.Upsert_Dim_Channel works well in SQL Server Management Studio (SSMS) because SSMS understands the EXEC syntax. However, in PySpark, the JDBC driver expects a tabular result, which can cause syntax errors for non-query statements like EXEC. Also, the stored procedure might not return a result set, whicPySpark’s spark.read.jdbc expects by default.
To execute the stored procedure dbo.Upsert_Dim_Channel in Microsoft Fabric using PySpark, you need to bypass the spark.read.jdbc limitation by using the JDBC driver’s native Java SQL capabilities (via java.sql.DriverManager) to execute the stored procedure directly.
If this response resolves your query, kindly mark it as Accepted Solution to help other community members. A Kudos is also appreciated if you found the response helpful.
Thank You!
User | Count |
---|---|
14 | |
9 | |
5 | |
4 | |
2 |
User | Count |
---|---|
44 | |
23 | |
17 | |
16 | |
12 |