【问题标题】:Decrease the output of bcp export减少 bcp export 的输出
【发布时间】:2013-10-27 03:29:01
【问题描述】:

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

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

我尝试使用bcp out 传递-b 开关,但它总是以1000 个为一组导出它们,并且通过grepingseding 过滤行将花费太多时间。 感谢您的帮助。

【问题讨论】:

  • 我不确定这里的问题是什么。您的 BCP 是否无法正常工作,或者您是否尝试在 BCP 输出消息中查看某些数据需要查看?
  • 我想减少此类行 1000 rows successfully bulk-copied to host-file. Total received: 2000 的出现,因为我们将 bcp 的输出记录在一个文件中,并且在某些情况下有数百万行,因此日志文件变得非常大,我想在不影响 bcp 性能的情况下减小日志文件的大小。
  • 好的,这就澄清了。不确定是否有可能……祝你好运。
  • 这是bcp 的新成员吗?我在 linux 机器上使用 bcp 时遇到了同样的问题,但我在使用 bcp 且不产生此输出的旧服务器(仅一年)上有另一个进程。两者都使用linux centos7。

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


【解决方案1】:

在 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

【讨论】:

  • 问题不在于输出,而在于输出的长度,我想减少输出但我需要它,您的解决方案将从日志文件中删除所有输出。
【解决方案2】:

阻止命令行输出:

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

【讨论】:

    【解决方案3】:
    1. 您可以使用 >

      将输出重定向到文件

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

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

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

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

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

    【讨论】:

    • 我正在使用 bcp Version: 17.3.0000.1 from centos7 mssql-tool package 它告诉我 -o 不是一个有效的选项。在什么版本和操作系统上使用它?
    【解决方案4】:

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

    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】:

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

      只需附加 |在命令末尾输出 null。

      例如:

      bcp  xxxxxxxxxxxxxxxxxxxx   | out-null
      

      【讨论】:

        猜你喜欢
        • 2021-07-01
        • 2017-08-17
        • 2019-04-18
        • 1970-01-01
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多