I am trying to run BCP commands on a remote server using python. I need the data, as well as the xml format file for each table. Since my first table query still fails, i have yet to try the format command.
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)
Executing the above script, always produces the following error:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '.'. (102) (SQLExecDirectW)")
I'm able to send the command in a batch script and it works fine, but my superiors would really prefer i use python for this. The executing and remote servers are trusted within the network, so the user/password parameters are omitted.
The following working batch script that loops thru a textfile of tablenames, running the below commands for each one. %%x = the name of a table in the database, with delimiters of rows |R^| and columns |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