SQl Server 2014 BCP BulkCopy删除最后一个空白行

时间:2015-01-23 15:22:23

标签: sql-server bcp xp-cmdshell blank-line

请问是否有人可以建议是否有一种方法可以用来阻止SQL Server的BCP批量复制命令创建最终的回车符,从而在文件的末尾留下空行?

我一直在谷歌搜索,但无法找到合适的答案。

我在存储过程中使用的代码如下所示。省略了查询和文件路径,但SQL变量仍然存在:

    /* Prepare the command */
    DECLARE @Command VARCHAR(4000);
    SELECT  @Command = 
                'bcp "'             /* Bulk Copy command */
    +           @Query              /* The query to output */
    +           '" queryout '       /* Output the results of the query */
    +           @FullFilePath       /* The file path to write to */
    +           ' -c -t"|" -T -S'   /* Switches (See below) */
    +           @@servername;       /* The server name */


    /*
        BCP Command Swtitches
            -c Output in ASCII with the default field terminator (tab) and row terminator (crlf)
            -t override the field terminator with "|"
            -T use a trusted connection. Note that U –P may be used for username/password
            -S connect to this server to execute the command
    */

    /* Execute the command */
    exec master..xp_cmdshell @Command;

谢谢。

1 个答案:

答案 0 :(得分:1)

我正在与同样的事情作斗争,我唯一可以解决这个问题的方法是首先在BCP查询中使用 NOT USE custom -t / t 列分隔符。

所以我原来的BCP导出语法:

bcp "select 1 from [myDB].[dbo].[db_status] WHERE db_idx = 0" queryout c:\db_migration_log.txt -c -t/t -S127.0.0.1,2044 -Uuser -Ppassword

必须改为:

bcp "select 1 from [myDB].[dbo].[db_status] WHERE db_idx = 0" queryout c:\db_migration_log.txt -c -S127.0.0.1,2044 -Uuser -Ppassword

然后我的导入查询工作正常(忽略最后一行),如下所示:

SET QUOTED IDENTIFIER OFF
SET NOCOUNT ON
BULK INSERT [myDB].[dbo].[mayTable] FROM 'c:\myfile.csv'
WITH
(
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)
相关问题