减少bcp导出的输出

时间:2013-10-18 10:12:33

标签: sql-server-2008 export-to-csv bcp

在我们的项目中,我们使用bcp命令导出大约数百万行并将输出记录到输出文件中。 对于bcp导入,我可以使用指定no的bcp开关来控制-b命令的输出。批量导入的行数。输出是这样的:

Starting copy...
1000 rows sent to SQL Server. Total sent: 1000
1000 rows sent to SQL Server. Total sent: 2000
1000 rows sent to SQL Server. Total sent: 3000
1000 rows sent to SQL Server. Total sent: 4000
1000 rows sent to SQL Server. Total sent: 5000
1000 rows sent to SQL Server. Total sent: 6000
1000 rows sent to SQL Server. Total sent: 7000
1000 rows sent to SQL Server. Total sent: 8000
1000 rows sent to SQL Server. Total sent: 9000
1000 rows sent to SQL Server. Total sent: 10000
1000 rows sent to SQL Server. Total sent: 11000
1000 rows sent to SQL Server. Total sent: 12000
SQLState = 22001, NativeError = 0
通过增加使用-b开关发送的号码,可以轻松减少

Starting copy...
10000 rows sent to SQL Server. Total sent: 10000
SQLState = 22001, NativeError = 0

12406 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total     : 75     Average : (165413.3 rows per sec.)

但是对于bcp导出我无法控制输出,对于一百万行,日志变得太大,例如。以下命令

bcp  Temp.dbo.TestTable out outdata.txt -t , -f file.fmt -S Server -U user-P password -m 10

输出:

Starting copy...
1000 rows successfully bulk-copied to host-file. Total received: 1000
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 11 for SQL Server]Warning: BCP import with a format file will convert empty strings in delimited columns to NULL.
1000 rows successfully bulk-copied to host-file. Total received: 2000
1000 rows successfully bulk-copied to host-file. Total received: 3000
1000 rows successfully bulk-copied to host-file. Total received: 4000
1000 rows successfully bulk-copied to host-file. Total received: 5000
1000 rows successfully bulk-copied to host-file. Total received: 6000
1000 rows successfully bulk-copied to host-file. Total received: 7000
1000 rows successfully bulk-copied to host-file. Total received: 8000
1000 rows successfully bulk-copied to host-file. Total received: 9000
1000 rows successfully bulk-copied to host-file. Total received: 10000
1000 rows successfully bulk-copied to host-file. Total received: 11000
1000 rows successfully bulk-copied to host-file. Total received: 12000
1000 rows successfully bulk-copied to host-file. Total received: 13000
1000 rows successfully bulk-copied to host-file. Total received: 14000
1000 rows successfully bulk-copied to host-file. Total received: 15000
1000 rows successfully bulk-copied to host-file. Total received: 16000
1000 rows successfully bulk-copied to host-file. Total received: 17000
1000 rows successfully bulk-copied to host-file. Total received: 18000
1000 rows successfully bulk-copied to host-file. Total received: 19000
1000 rows successfully bulk-copied to host-file. Total received: 20000
1000 rows successfully bulk-copied to host-file. Total received: 21000
1000 rows successfully bulk-copied to host-file. Total received: 22000

我尝试将-b切换为bcp out,但它始终以1000个批量导出它们并按grepingseding过滤行,这将耗费太多时间。 谢谢你的帮助。

5 个答案:

答案 0 :(得分:2)

在bcp中似乎没有解决方案。但是,有一种解决方法;将bcp命令行打包到xp_cmdshell语句中并指定no_output选项:

EXEC xp_cmdshell "bcp  Temp.dbo.TestTable out outdata.txt -t , -f file.fmt -S Server -U user-P password -m 10", no_output

来源:click here

答案 1 :(得分:0)

阻止命令行输出:

bcp Temp.dbo.TestTable out outdata.txt -t , -f file.fmt -S Server -U user-P password -m 10>nul

答案 2 :(得分:0)

  1. 您可以使用>

    将输出重定向到文件

    bcp sometable out outfile -S Server -U user -P password > export.log

    请注意> export.log 到最后。这将使用log填充export.log。因此,如果您的命令失败,您可以检查。有关此方法的详细信息,请参阅here

  2. bcp还提供输出参数-o

    bcp sometable out outfile -S Server -U user -P password -o export.log

    这次最后注意 -o export.log

答案 3 :(得分:0)

我知道这很旧,但是我偶然发现了它以寻找相同的答案,并且发现这里没有好的答案。我想到了以下内容,这些内容使我得到了想要的东西,并认为我会在这里发帖,以防它对其他人有所帮助:

set @BCP_CMD = 'bcp ...etc...'; -- declare and set this appropriately
DROP TABLE IF EXISTS #bcpOutput;
CREATE TABLE #bcpOutput ([BCP_Output] varchar(2048)); 
Insert into #bcpOutput ([BCP_Output]) EXECUTE master..xp_cmdshell @BCP_CMD;
SELECT BCP_Output FROM #bcpOutput where 
-- Here I filter out Blank lines, all those "rows successfully..." lines, etc.
BCP_Output not like '' 
    AND BCP_Output not like '%rows successfully%' 
    AND BCP_Output not like '%S1000%' 
    AND BCP_Output not like '%empty strings in delimited%' 
    AND BCP_Output not like '%Starting copy%' 
    AND BCP_Output not like '%Network packet size%' ;

这将产生:

BCP_Output
69673 rows copied.
Clock Time (ms.) Total     : 406    Average : (171608.38 rows per sec.)

从技术上讲,另一种方法“行之有效”:追加

 | grep -v -e "rows successfully\|Starting copy\|...and so on..."

到您的“ bcp ...”命令。 但是,对我而言,它将<5秒的bcp操作转换为将近30秒,这是不可接受的。也许在您的环境中,向grep传递管道效果更好。可能值得一试。

答案 4 :(得分:0)

非常简单的解决方案是使用out-null

只需追加 |命令末尾的 out-null。

例如:

let objString = '{ one: [1, 2, 3], num: 1 }';