You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

在SQL Server的sp_execute_external_script中pyodbc连接失败求助

Troubleshooting SQL Server Connection Issues in Python via sp_execute_external_script

Hey there! Let's break down why your Python connection works locally but fails when run through sp_execute_external_script—this is almost always related to the context (service account, network settings) that the external script runs under. Here are the most likely fixes to try:

1. Force TCP/IP in Your Connection String

The error specifically mentions a Named Pipes connection failure, even though you specified an IP address. By default, the ODBC driver might fall back to Named Pipes in the SQL Server Launchpad environment, which might not be enabled or reachable on your target server.

Update your connection string to explicitly use TCP/IP and specify the port (1433 is the default; adjust if your target uses a custom port):

conn_str = (
  r'DRIVER={ODBC Driver 17 for SQL Server};'
  r'SERVER=tcp:xxx.xxx.xxx.xxx,1433;'
  r'DATABASE=xxxx;'
  r'UID=xxxxxx;'
  r'PWD=xxxx;'
)

2. Verify the SQL Server Launchpad Service Account's Network Access

When you run sp_execute_external_script, Python code runs under the SQL Server Launchpad service account (default: NT Service\MSSQLLaunchpad), not your local user account. This account might not have permission to reach the target SQL Server over the network.

To test this:

  • Use a tool like PsExec to open a command prompt as the Launchpad account:
    psexec -s -i cmd.exe
    
  • From that prompt, test the connection to your target server using sqlcmd:
    sqlcmd -S tcp:xxx.xxx.xxx.xxx,1433 -U xxxxxx -P xxxx
    

If this fails, you'll need to adjust network/firewall settings to allow the Launchpad server to communicate with the target SQL Server.

3. Check Target SQL Server Remote Connection Settings

Double-check that your target SQL Server is configured to accept remote connections:

  • Open SQL Server Configuration Manager on the target server.
  • Under SQL Server Network Configuration, enable the TCP/IP protocol for your target instance.
  • In SQL Server Management Studio, right-click the target server → PropertiesConnections, ensure "Allow remote connections to this server" is checked.
  • Confirm the target server's firewall allows inbound connections on your SQL Server port (1433 or custom) from the machine running the external script.

4. Try Windows Authentication (If Domain/Trust Relationships Exist)

If your source and target servers are in the same domain (or have a trust relationship), switching to Windows Authentication might simplify permissions. Update your connection string to use trusted auth:

conn_str = (
  r'DRIVER={ODBC Driver 17 for SQL Server};'
  r'SERVER=tcp:xxx.xxx.xxx.xxx,1433;'
  r'DATABASE=xxxx;'
  r'Trusted_Connection=yes;'
)

Just make sure the NT Service\MSSQLLaunchpad account (or a domain account you've configured the Launchpad service to use) has a login and appropriate permissions on the target SQL Server.

Start with the first fix (forcing TCP/IP)—that's the quickest win for Named Pipes errors. If that doesn't work, move through the other steps to narrow down the permission/network issue.

内容的提问来源于stack exchange,提问作者BuzzWord

火山引擎 最新活动