问题
All, I'm trying to write a query that I can run daily from a batch file on a SQL server 2008 instance.
I've never used BCP before but after looking at some examples online, I've tried to create a real basic query to test the process & permissions on the machine before I look to widen the select query to the required dataset.
I'm using:
bcp
"SELECT manifest_dt from EasyShip_050300.airwaybills"
queryout C:\Shares\DHL-EXPORT-TEST\file.txt -SGRENSON-CARRIER\DHLEASYSHIP -c -T
It appears to match nearly every example I can find online but everytime I execute this query I receive the error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'queryout'.
If anybody has any pointers it would be appreciated.
回答1:
I believe you should have the output file path in speech marks also, something like this;
bcp
"SELECT manifest_dt from EasyShip_050300.airwaybills"
queryout "C:\Shares\DHL-EXPORT-TEST\file.txt" -S GRENSON-CARRIER\DHLEASYSHIP -c -T
Also, put a space after your -S server declaration to see if that helps.
回答2:
Let me reply with two generic commands that I know to work in my environment (using trusted connection). See if they work on yours too, and work from there:
-- default row separator, column separator
DECLARE @stmt VARCHAR(8000);
SET @stmt=
'BCP '+
'"SELECT*FROM INFORMATION_SCHEMA.TABLES" '+
'QUERYOUT "C:\Temp\information_schema.txt" '+
'-c -T -S ' + @@SERVERNAME + ' -d ' + DB_NAME();
EXEC master.sys.xp_cmdshell @stmt;
-- comma separated:
DECLARE @stmt_c VARCHAR(8000);
SET @stmt_c=
'BCP '+
'"SELECT*FROM '+QUOTENAME(DB_NAME())+'.INFORMATION_SCHEMA.TABLES" '+
'QUERYOUT "C:\Temp\information_schema.csv" '+
'-c -t, -T -S ' + @@SERVERNAME;
EXEC master.sys.xp_cmdshell @stmt_c;
If they work, SELECT @stmt_c; SELECT @stmt;
and see how they are formed. Then see what the difference is with your commands.
回答3:
BCP is a command-line utility so you need to run it from a command-prompt instead of a SQL query window.
来源:https://stackoverflow.com/questions/40590017/bcp-syntax-issues