批量插入期间出现额外字符

发布于 2024-10-07 08:06:32 字数 956 浏览 8 评论 0原文

我正在尝试将 csv 文件中的第一行批量插入到只有一列的表中。 但我在开头得到了一些额外的字符('n++'),如下所示:

n++First Column;Second Column;Third Column;Fourth Column;Fifth Columnm;Sixth Column

CSV 文件内容如下:

First Column;Second Column;Third Column;Fourth Column;Fifth Columnm;Sixth Column

您可以找到 test.csv 文件 here

这是我用来获取表中第一行数据的代码

declare @importSQL nvarchar(2000)
declare @tempstr varchar(max)
declare @path varchar(100)  

SET @path = 'D:\test.csv'    

CREATE TABLE #tbl (line VARCHAR(max))

SET @importSQL = 
'BULK INSERT #tbl 
FROM ''' + @path + ''' 
WITH ( 
LASTROW = 1,
FIELDTERMINATOR = ''\n'',
ROWTERMINATOR = ''\n''
)' 

EXEC sp_executesql @stmt=@importSQL 

SET @tempstr = (SELECT TOP 1 RTRIM(REPLACE(Line, CHAR(9), ';')) FROM #tbl)

print @tempstr
drop table #tbl

知道这个额外的“n++”在哪里吗来自?

I am trying to bulk insert the first row from a csv file into a table with only one column.
But I am getting some extra characters('n++') in the begining like this:

n++First Column;Second Column;Third Column;Fourth Column;Fifth Columnm;Sixth Column

CSV file contents are like:

First Column;Second Column;Third Column;Fourth Column;Fifth Columnm;Sixth Column

You can find the test.csv file here

And this is the code I am using to get the first row data in a table

declare @importSQL nvarchar(2000)
declare @tempstr varchar(max)
declare @path varchar(100)  

SET @path = 'D:\test.csv'    

CREATE TABLE #tbl (line VARCHAR(max))

SET @importSQL = 
'BULK INSERT #tbl 
FROM ''' + @path + ''' 
WITH ( 
LASTROW = 1,
FIELDTERMINATOR = ''\n'',
ROWTERMINATOR = ''\n''
)' 

EXEC sp_executesql @stmt=@importSQL 

SET @tempstr = (SELECT TOP 1 RTRIM(REPLACE(Line, CHAR(9), ';')) FROM #tbl)

print @tempstr
drop table #tbl

Any idea where this extra 'n++' is coming from?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假装爱人 2024-10-14 08:06:32

It seems UTF-8 files are not supported by SQL Server 2005 and 2008, it will only be available in version 11!

https://connect.microsoft.com/SQLServer/feedback/details/370419/bulk-insert-and-bcp-does-not-recognize-codepage-65001

写下不归期 2024-10-14 08:06:32

额外的字符是由编码引起的。您可以使用记事本将编码格式从UTF-8更改为Unicode。这删除了第一行上的“n++”。

The extra charectors are caused by the encoding. You can use used notepad to change the encoding format from UTF-8 to Unicode. This removed the 'n++' on the first row.

英雄似剑 2024-10-14 08:06:32

它可能是正在选取的 Unicode 字节顺序标记

我建议您尝试将 DATAFILETYPE 选项设置为语句的一部分。有关更多详细信息,请参阅 MSDN 文档:http://msdn。 microsoft.com/en-us/library/aa173832%28SQL.80%29.aspx

It might be the Unicode Byte Order Mark that are being picked up.

I suggest your try setting the DATAFILETYPE option as part of your statement. See MSDN documentation for more detail: http://msdn.microsoft.com/en-us/library/aa173832%28SQL.80%29.aspx

哭泣的笑容 2024-10-14 08:06:32

不幸的是,旧的 SQL Server 版本不支持 utf-8。将代码页参数添加到批量插入方法。在您的问题中,请更改现有的代码。

SET @importSQL = 
'BULK INSERT #tbl 
    FROM ''' + @path + ''' 
    WITH ( LASTROW = 1, 
           FIELDTERMINATOR = ''\n'', 
           ROWTERMINATOR = ''\n'' , 
           CODEPAGE=''65001'')'

请注意,您的文件必须采用 utf-8 格式。
但问题是,如果您将服务器从 2005 年升级到 2008 年,则不支持代码页 65001(utf-8),然后您将收到“不支持代码页”消息

Unfortunatelly, Old SQL Server versions not supports utf-8. Add the codepage parameter to bulk insert method. In your question please change your code as exists.

SET @importSQL = 
'BULK INSERT #tbl 
    FROM ''' + @path + ''' 
    WITH ( LASTROW = 1, 
           FIELDTERMINATOR = ''\n'', 
           ROWTERMINATOR = ''\n'' , 
           CODEPAGE=''65001'')'

Note that, your file must be in utf-8 format.
But the problem there is, if you're upgrade your server from 2005 to 2008 the codepage 65001(utf-8) not supported and then you will get the " codepage not supported"message

梦断已成空 2024-10-14 08:06:32

在 SQL Server 的更高版本中,您可以在命令中添加“-C 65001”以告诉它使用 utf-8 编码。这将从第一行中删除 n++。这是大写的 C。当然,当您键入命令时,不要包含引号。

In later versions of SQL server you can add '-C 65001' to the command to tell it to use utf-8 encoding. This will remove the n++ from the first line. That is a capital C. Of course when you type the command don't include the quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文