我需要BCP一个表,其中包含一列中的三行。例如:
ID Name Comments
---------------------
1 | Sample | "Hai.
Hello.
How."
但最终的BCP输出文件有三行而不是一行:
1^|^Sample^|^Hai
Hello^|^
How^|^
BCP查询:
bcp table out table_one.bcp -c -t "^|^" -Q -User -Password.
答案 0 :(得分:1)
如果源文本中有Windows行分隔符({CR}{LF}
)并且不想在输出文件中使用这些分隔符,则可以在查询中用您喜欢的内容替换它们(例如空格)
请考虑以下代码段。源数据包含Windows行分隔符。在查询中,这些被替换为空格。您将在输出文件中看到源文本不再有多行。您应该能够从SSMS运行此代码段。
CREATE TABLE ##t(header INT IDENTITY(1,1) PRIMARY KEY,txt VARCHAR(1024));
INSERT INTO ##t(txt)VALUES
('This is the first line.
This is the second line.
This is the third line'),
('Again the first line.
Again the second line.
Again the third line.');
DECLARE @cmd NVARCHAR(4000)='BCP "SELECT header,REPLACE(txt,CHAR(13)+CHAR(10),'' '') FROM ##t" QUERYOUT "C:\Temp\test_txt.txt" -c -T -S ' + @@SERVERNAME + ' -d ' + DB_NAME();
EXECUTE master.sys.xp_cmdshell @cmd;
DROP TABLE ##t;
输出文件C:\Temp\test_txt.txt
:
1 This is the first line. This is the second line. This is the third line
2 Again the first line. Again the second line. Again the third line.
答案 1 :(得分:0)
尝试稍微不同的命令
bcp "select * from db..table" queryout "C:\dir\file.bcp" -n -T -SServer