You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python脚本访问Azure Synapse无服务器SQL Pool外部表时报错的问题求助

使用Python脚本访问Azure Synapse无服务器SQL Pool外部表时报错的问题求助

大家好,我在使用Azure Synapse Analytics时碰到了一个棘手的问题,想请各位帮忙排查下。

我的需求是用Python3脚本读取Synapse中的表数据,当前用的是无服务器SQL池,已经基于ADLS Gen2上的Parquet文件创建了外部表,并且配置好了链接服务。在Synapse的「开发」模块里用SQL脚本查询这些表完全正常,但换成Python脚本访问时就报错了。

报错信息

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]External table 'Employee_table' is not accessible because location does not exist or it is used by another process. (16562) (SQLExecDirectW)")

我使用的Python脚本

import pyodbc
import pandas as pd
import sys
import logging
import traceback

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def main():
    try:
        # Set up connection to Synapse
        server = 'serverless-synapse-analytics-ondemand.sql.azuresynapse.net' #serverless endpoint
        database = 'synapse_analytics'
        username = 'liveuser'
        password = 'StrongPassword!@#0'
        driver= '{ODBC Driver 18 for SQL Server}'

        conn_str = 'DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password
        conn = pyodbc.connect(conn_str)
        cursor = conn.cursor()

        #print success message if connect is successful
        if conn:
            print('Connection established')

        # Execute a query and print the top 100 result rows
        sql = """select top 10 * from SAM_table"""
        cursor.execute(sql)
        rows = cursor.fetchall()
        print(rows.head(3))

        # Close the connection
        conn.close()

    except Exception as e:
        logger.error(f'Error: {e}')
        logger.error(f'Trace: {traceback.format_exc()}')

if __name__ == '__main__':
    main()

补充说明

脚本能成功连接到Synapse(会输出Connection established),但执行SQL查询的时候就触发了上面的错误。我想知道自己哪里考虑漏了?或者应该怎么修改脚本才能正常读取数据?

备注:内容来源于stack exchange,提问作者Manish Agarwal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 06:18:05