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
mike9999
Advocate I
Advocate I

Notebook to query Synapse Serverless SQL

I'm struggling with Fabric Notebooks - attempting a basic python notebook not a spark notebook.

 

I want to connect and pull data from Synapse Serverless SQL. Presumably, this should be fairly well integrated with Fabric and easy to pull into a notebook (and potentially save to a lakehouse, etc.). Here is some sample error but I get a kernel error on the connection (I successfully get a token).

 

Error:

Kernel died: Kernel python3.11 has died, please restart the kernel.

Diagnostic Info:
session id: 208aaaf3-f0a0-40f3-977f-943d60c906e1
pid: 989
exit code: -11 (Invalid memory reference)
cluster name: 

Here is my sample code:

import pyodbc

# Get token using notebookutils (Synapse)
access_token = notebookutils.credentials.getToken("https://database.windows.net/.default")

# Connection string with access token
server = 'your-server.database.windows.net'
database = 'your-database'
driver = '{ODBC Driver 18 for SQL Server}'  # or latest version available

connection_string = f"""
    Driver={driver};
    Server={server};
    Database={database};
    Encrypt=yes;
    TrustServerCertificate=no;
    Connection Timeout=30;
"""

# Create connection with access token
conn = pyodbc.connect(connection_string, attrs_before={
    1256: access_token  # SQL_COPT_SS_ACCESS_TOKEN
})

 Any suggestions?

1 ACCEPTED SOLUTION

Here is what worked in the end for me, maybe this helps someone else.

 

def query_synapse(db, query_string):
    """
        Function to query synapse 
    """
    # Synapse serverless SQL endpoint
    server = "<yourserver>.sql.azuresynapse.net"
    database = "<yourdb>"
    driver = "ODBC Driver 18 for SQL Server"

    connection_string = f"""
        Driver={driver};
        Server={server};
        Database={database};
    """
    token = notebookutils.credentials.getToken('https://database.windows.net/').encode("UTF-16-LE")
    token_struct = struct.pack(f"<I{len(token)}s", len(token), token)
    conn = pyodbc.connect(connection_string, attrs_before={1256:token_struct})
        
    cursor = conn.cursor()
    cursor.execute(query_string)

    # Get column names
    columns = [column[0] for column in cursor.description]

    # Fetch data
    rows = cursor.fetchall()

    # Convert to DataFrame
    df = pd.DataFrame.from_records(rows, columns=columns)

    return df

 

 

View solution in original post

3 REPLIES 3
mike9999
Advocate I
Advocate I

I gave it a try but doesn't allow with an authentication method and a token- removing the auth method but keeping your encoding also doesn't work.

Error: ('FA005', '[FA005] [Microsoft][ODBC Driver 18 for SQL Server]Cannot use Access Token with any of the following options: Authentication, Integrated Security, User, Password. (0) (SQLDriverConnect)')

 

Here is what worked in the end for me, maybe this helps someone else.

 

def query_synapse(db, query_string):
    """
        Function to query synapse 
    """
    # Synapse serverless SQL endpoint
    server = "<yourserver>.sql.azuresynapse.net"
    database = "<yourdb>"
    driver = "ODBC Driver 18 for SQL Server"

    connection_string = f"""
        Driver={driver};
        Server={server};
        Database={database};
    """
    token = notebookutils.credentials.getToken('https://database.windows.net/').encode("UTF-16-LE")
    token_struct = struct.pack(f"<I{len(token)}s", len(token), token)
    conn = pyodbc.connect(connection_string, attrs_before={1256:token_struct})
        
    cursor = conn.cursor()
    cursor.execute(query_string)

    # Get column names
    columns = [column[0] for column in cursor.description]

    # Fetch data
    rows = cursor.fetchall()

    # Convert to DataFrame
    df = pd.DataFrame.from_records(rows, columns=columns)

    return df

 

 

nilendraFabric
Super User
Super User

Hi @mike9999 

 

worth trying 

 


Encode token for ODBC driver
token_bytes = access_token.encode("UTF-16-LE")
token_struct = struct.pack(f'<I{len(token_bytes)}s', len(token_bytes), token_bytes)

 

 

 

connection_string = f"""
Driver={driver};
Server={server};
Database={database};
Encrypt=yes;
TrustServerCertificate=no;
Authentication=ActiveDirectoryInteractive; # Critical for AAD auth
Connection Timeout=30;
"""

 

 

conn = pyodbc.connect(
connection_string,
attrs_before={1256: token_struct} # SQL_COPT_SS_ACCESS_TOKEN
)

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.