6

在我们的项目中,我们使用 bcp 命令导出大约数百万行并将输出记录到输出文件中。对于 bcp 导入,我可以使用指定编号的开关来控制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

可以通过增加使用-bswitch 发送的数量来轻松减少:

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使用 switch 传递,bcp out但它总是以 1000 个批量导出它们并过滤行,greping否则seding它们将花费太多时间。谢谢你的帮助。

4

5 回答 5

4

在 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

来源:点击这里

于 2014-04-27T14:15:39.117 回答
2
  1. 您可以使用 > 将输出重定向到文件

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

    注意最后的> export.log位。这将用日志填充 export.log。因此,如果您的命令失败,您可以检查。有关此方法的更多详细信息,请参见此处

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

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

    这次注意最后的-o export.log

于 2017-05-23T13:02:07.807 回答
1

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

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 效果更好。也许值得尝试一下。

于 2019-02-27T16:57:54.187 回答
1

阻止命令行输出:

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

于 2017-02-10T23:08:21.910 回答
0

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

只需附加 | 在命令结尾处为空。

例如:

bcp  xxxxxxxxxxxxxxxxxxxx   | out-null
于 2021-04-07T02:29:49.987 回答