问题
I'm copying data from SQL to text file using bcp tool with following command:
bcp.exe "exec <StoredProcedure>" queryout "temp.txt" -T -r\n -c
This command runs as expected on test environment and creates/updates required file without additional prompts.
But when it's run in client's environment, bcp additionally prompts for field length, prefix and field terminator- so when command is run from SQL Agent job, it get's stuck in infinite wait.
Any ideas why is this happening?
Based on bcp documentation, when -c parameter is used - it shouldn't prompt for type, etc. and should use char as the storage type, with no prefix, and \t as field terminator.
回答1:
I would guess the somewhere between your test environment and the client's environment's actual execution of the command that the "/n" is getting turned into a real new-line. Try putting the "-c" before the "-r" and see what happens.
回答2:
There is no need to specify the record delimiter, so remove the -r\n
. The -c
option will use tab for columns and carriage return and line feed for records (same as specifying -t\t -r\r\n
). Otherwise like the other poster stated, make sure the -c
is specified first
Side note: I recommend that as a practice (not as a solution) that you output the format file using -f"yourpathandfilename.fmt"
. On the client side you use the -f
param with import as well. This way if there is a difference between the source and target you can manually tweak the format file.
来源:https://stackoverflow.com/questions/32316921/writing-data-to-text-file-using-bcp-prompts-for-file-storage-type