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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
yashaswi_raj
Frequent Visitor

How to Connect to Azure SQL DB from Fabric Notebook

Can we connect to azure sql db from fabric notebook and insert data using stored procedures from fabric notebook itself

1 ACCEPTED SOLUTION
tayloramy
Solution Supplier
Solution Supplier

Hi @yashaswi_raj

 

Yes. You can connect from a Fabric notebook to Azure SQL DB and call stored procedures. The most reliable pattern is Python + pyodbc with a Microsoft Entra ID access token.

1) Prereqs

  • Networking open to Fabric: either enable public connectivity (server firewall or "Allow Azure services...") or use Fabric Managed Private Endpoints (MPE). See Azure SQL network controls docs and Fabric MPE overview docs (how-to with notebook samples docs).
  • Permissions: your Entra user or service principal must exist in the database and have EXECUTE rights on the proc, e.g. CREATE USER [app-or-user] FROM EXTERNAL PROVIDER; GRANT EXECUTE ON SCHEMA::dbo TO [app-or-user]; (CREATE USER, GRANT on stored procedures).

2) Notebook code 

# In a Python cell
%pip install pyodbc

import struct, pyodbc
from notebookutils import mssparkutils  # alias of mssparkutils in Fabric

server   = "<yourserver>.database.windows.net"
database = "<yourdb>"

# Get Entra access token for Azure SQL
token = mssparkutils.credentials.getToken("https://database.windows.net/")  # Fabric NotebookUtils <https://learn.microsoft.com/en-us/fabric/data-engineering/notebook-utilities>

# Convert token for ODBC Driver 18 (SQL_COPT_SS_ACCESS_TOKEN = 1256)
exptoken = b"".join(bytes([c]) + b"\x00" for c in token.encode("utf-8"))
token_struct = struct.pack("=i", len(exptoken)) + exptoken

conn_str = (
    "Driver={ODBC Driver 18 for SQL Server};"
    f"Server={server};Database={database};"
    "

 If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, please mark this as the solution.

View solution in original post

4 REPLIES 4
tayloramy
Solution Supplier
Solution Supplier

Hi @yashaswi_raj

 

Yes. You can connect from a Fabric notebook to Azure SQL DB and call stored procedures. The most reliable pattern is Python + pyodbc with a Microsoft Entra ID access token.

1) Prereqs

  • Networking open to Fabric: either enable public connectivity (server firewall or "Allow Azure services...") or use Fabric Managed Private Endpoints (MPE). See Azure SQL network controls docs and Fabric MPE overview docs (how-to with notebook samples docs).
  • Permissions: your Entra user or service principal must exist in the database and have EXECUTE rights on the proc, e.g. CREATE USER [app-or-user] FROM EXTERNAL PROVIDER; GRANT EXECUTE ON SCHEMA::dbo TO [app-or-user]; (CREATE USER, GRANT on stored procedures).

2) Notebook code 

# In a Python cell
%pip install pyodbc

import struct, pyodbc
from notebookutils import mssparkutils  # alias of mssparkutils in Fabric

server   = "<yourserver>.database.windows.net"
database = "<yourdb>"

# Get Entra access token for Azure SQL
token = mssparkutils.credentials.getToken("https://database.windows.net/")  # Fabric NotebookUtils <https://learn.microsoft.com/en-us/fabric/data-engineering/notebook-utilities>

# Convert token for ODBC Driver 18 (SQL_COPT_SS_ACCESS_TOKEN = 1256)
exptoken = b"".join(bytes([c]) + b"\x00" for c in token.encode("utf-8"))
token_struct = struct.pack("=i", len(exptoken)) + exptoken

conn_str = (
    "Driver={ODBC Driver 18 for SQL Server};"
    f"Server={server};Database={database};"
    "

 If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, please mark this as the solution.

thanks for the answer, @tayloramy 

 

So the Fabric workspace identity can't be used as a authentication . right?

Hi @yashaswi_raj

 

A fabric workspace identity is a service principal in azure, so if it is granted access to the Azure SQL server resource, it should be able to be used anywhere where a service principal is supported. 

 

Workspace identity - Microsoft Fabric | Microsoft Learn

 

If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, please mark this as the solution.

Hi @yashaswi_raj  ,
Thanks for reaching out to the Microsoft fabric community forum. 

I would also take a moment to thank  @tayloramy  , for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference. 
Yes, The Fabric workspace identity is basically a service principal in Azure. If you add it as a user in your Azure SQL Database and give it the right permissions (like EXECUTE on your stored procedures), you can use it for authentication from the notebook, just like any other service principal.

Best Regards, 
Community Support Team

Helpful resources

Announcements
August Fabric Update Carousel

Fabric Monthly Update - August 2025

Check out the August 2025 Fabric update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.