SQL 服务器在查询结果中添加额外的特殊字符

SQL Server adding extra special characters in query result

我正在尝试使用 SQL 服务器中的 BCP 命令提取文件中的一些记录。但是,当生成文件时,每列的结果之间有提取物 spaces。

为了尝试,我只写了基本的 SQL 查询就这么简单

select 'ABC', 40, 'TEST','NOTWORKING'

当我们复制上述查询的输出并将其粘贴到记事本中时,输出结果为

ABC 40  TEST    NOTWORKING

注意到每个值之间的 space 了吗?系统使用 BCP 命令生成的文件在输出文件中也有相同的 space,这是不正确的。我想在输出文件中看到的是

ABC40TESTNOTWORKING

一定是什么导致了这个问题?我只是很惊讶地看到这样奇怪的问题,并希望它可以通过一些更改或设置来解决。请帮忙。

Sample BCP command
     EXEC xp_cmdshell  'bcp "select ''ABC'', 40, ''TEST'',''NOTWORKING''" queryout "E:\Testfile.txt"  -c -T -S""'
Output in the File - Testfile.txt
     ABC    40  TEST    NOTWORKING

值之间可能有制表符。如果您想要单个值,请使用 concat():

select CONCAT('ABC', 40, 'TEST', 'NOTWORKING')

没问题。命令行没有字段终止符参数,因此使用默认值,一个制表符。这在文档中有描述:

-t field_term

Specifies the field terminator. The default is \t (tab character). Use this parameter to override the default field terminator. For more information, see Specify Field and Row Terminators (SQL Server).

If you specify the field terminator in hexadecimal notation in a bcp.exe command, the value will be truncated at 0x00. For example, if you specify 0x410041, 0x41 will be used.

If field_term begins with a hyphen (-) or a forward slash (/), do not include a space between -t and the field_term value.

link 指向整篇文章,其中解释了如何为每个批量操作使用终止符。

至于Copy/Paste操作,与SQL服务器无关。 SQL服务器没有UI,这是一个服务。我怀疑粘贴在记事本中的内容是从 SSMS 网格复制的。

SSMS 是一个 客户端 工具,就像其他工具一样。当您将数据从它复制到剪贴板时,它会决定将什么放在那里以及使用什么格式。该格式可以是纯文本,使用空格和制表符进行布局、RTF、HTML 等

使用制表符作为字段分隔符的纯文本可能是任何工具的最佳选择,因为它在一定程度上保留了视觉布局并且仅使用单个字符作为分隔符.也可以使用使用空格的固定长度布局,但这会添加很可能属于字段的字符。

编码和代码页

-c 使用用户的默认代码页导出数据。这意味着使用不同代码页(排序规则)存储在 varchar 字段中的文本可能会被破坏。不可见的 Unicode 字符也会被破坏并显示为其他内容,或显示为 ?.

-c

Performs the operation using a character data type. This option does not prompt for each field; it uses char as the storage type, without prefixes and with \t (tab character) as the field separator and \r\n (newline character) as the row terminator. -c is not compatible with -w.

最好使用 -w.

将文件导出为 UTF16

-w

Performs the bulk copy operation using Unicode characters. This option does not prompt for each field; it uses nchar as the storage type, no prefixes, \t (tab character) as the field separator, and \n (newline character) as the row terminator. -w is not compatible with -c.

可以使用 -C 参数指定代码页。例如,-C 1251 将使用 Windows' Latin1 代码页导出数据。 1253 将使用希腊代码页将其导出。

-C { ACP | OEM | RAW | code_page }

Specifies the code page of the data in the data file. code_page is relevant only if the data contains char, varchar, or text columns with character values greater than 127 or less than 32.

SQL Server 2016 及更高版本还可以使用 -C 65001 将文本导出为 UTF8。早期版本不支持UTF8。

Versions prior to version 13 (SQL Server 2016 (13.x)) do not support code page 65001 (UTF-8 encoding). Versions beginning with 13 can import UTF-8 encoding to earlier versions of SQL Server.

所有这些都在bcp's online documentation中描述。

这个主题对于任何数据库都非常重要,它在文档中有一整节描述 data format and considerations, using format files to specify different settings per column, and guidelines to ensure compatibility with other applications