0

请任何人都可以建议是否有一种方法可以用来停止 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;

谢谢你。

4

1 回答 1

1

我正在与同样的事情作斗争,我可以解决这个问题的唯一方法是首先在 BCP 查询中不使用 自定义 -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'
)
于 2015-12-14T20:15:39.980 回答