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

Get Fabric certified for FREE! Don't miss your chance! 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
Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Free Fabric Certifications

Free Fabric Certifications

Get Fabric certified for free! Don't miss your chance.

January Fabric Update Carousel

Fabric Monthly Update - January 2026

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.