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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
chetanhiwale
Resolver I
Resolver I

Query data from Fabric SQL DB using spark / python.

Hi Folks, 

I am trying to query data using Fabric SQL DB using python / pyspark. But I am not getting proper syntax to query. If anyone has , can someone share the syntax. 

Thanks and regards.

1 ACCEPTED SOLUTION
Tamanchu
Advocate V
Advocate V

Hi @chetanhiwale ,

You can query a Fabric SQL Database from a Spark notebook using JDBC. Here's a working example:

jdbc_url = "jdbc:sqlserver://<your-server>.database.fabric.microsoft.com:1433;database=<your-db>"

df = spark.read \
    .format("jdbc") \
    .option("url", jdbc_url) \
    .option("dbtable", "dbo.YourTable") \
    .option("authentication", "ActiveDirectoryInteractive") \
    .load()
df.show()
Alternatively, if your SQL DB is in the same workspace, you can use the synapsesql connector:
df = spark.read.synapsesql("YourDatabase.dbo.YourTable")

Did this help?
Drop a kudo so others can find it ! 

😉

View solution in original post

3 REPLIES 3
chetanhiwale
Resolver I
Resolver I

Hi @Tamanchu , 
I tried both both options but they are not working for me. Have you tried to use MS-SQL python module by Microsoft

Hi @chetanhiwale,

If the JDBC and synapsesql approaches didn't work, you can try using pyodbc directly in a Fabric notebook:

Tamanchu_0-1772699630910.png

Also, could you share the error messages you got with the JDBC and synapsesql methods? That would help narrow down the root cause.

Thanks!

 

Tamanchu
Advocate V
Advocate V

Hi @chetanhiwale ,

You can query a Fabric SQL Database from a Spark notebook using JDBC. Here's a working example:

jdbc_url = "jdbc:sqlserver://<your-server>.database.fabric.microsoft.com:1433;database=<your-db>"

df = spark.read \
    .format("jdbc") \
    .option("url", jdbc_url) \
    .option("dbtable", "dbo.YourTable") \
    .option("authentication", "ActiveDirectoryInteractive") \
    .load()
df.show()
Alternatively, if your SQL DB is in the same workspace, you can use the synapsesql connector:
df = spark.read.synapsesql("YourDatabase.dbo.YourTable")

Did this help?
Drop a kudo so others can find it ! 

😉

Helpful resources

Announcements
FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

March Fabric Update Carousel

Fabric Monthly Update - March 2026

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