我正在尝试使用python在远程服务器上运行BCP命令。我需要数据,以及每个表的xml格式文件。因为我的第一个表查询仍然失败,所以我还没有尝试format命令
import pyodbc
import sys
import subprocess
import os
import bcp
conn = pyodbc.connect('Driver={SQL Server};'
'Server=10.100.100.10;'
'Database=CompanyDatabase;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
command = 'bcp.exe CompanyDatabase.dbo.AnyTableName out D:\PROD\BCPLIST\CompanyDatabase_AnyTableName.csv -c -t "|C^|" -r "|R^|" -T -S 10.10.100.100.10'
cursor.execute(command)
执行上述脚本时,始终会产生以下错误:
pyodbc.ProgrammingError:('42000',“[42000][Microsoft][ODBC SQL Server驱动程序][SQL Server]在“.”附近的语法不正确。(102)(SQLExecDirectW)”)
我能够以批处理脚本的形式发送命令,并且工作正常,但我的上级更希望我使用python来完成此任务。执行服务器和远程服务器在网络中受信任,因此省略了用户/密码参数
下面的工作批处理脚本循环通过一个表名文本文件,为每个表名运行以下命令。%%x=数据库中表的名称,带有行| R^和列| C^的分隔符
bcp.exe CompanyDatabase.dbo.%%x out D:\PROD\BCPLIST\CompanyDatabase_%%x.csv -c -t "|C^|" -r "|R^|" -T -S 10.100.100.10
bcp.exe CompanyDatabase.dbo.%%x format nul -c -x -f D:\PROD\BCPLIST\CompanyDatabaseFormat_%%x.xml -t "|C^|" -r "|R^|" -T -S 10.100.100.10
扩展Panagiotis Kanavos的评论; 通过使用子流程包,您可以将整个bcp命令放入其中并绕过bcp实用程序
相关问题 更多 >
编程相关推荐