批量插入期间出现额外字符
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SQL Server 2005和2008似乎不支持UTF-8文件,只有版本11才支持!
https:/ /connect.microsoft.com/SQLServer/feedback/details/370419/bulk-insert-and-bcp-does-not-recognize-codepage-65001
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
额外的字符是由编码引起的。您可以使用记事本将编码格式从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.
它可能是正在选取的 Unicode 字节顺序标记。
我建议您尝试将
DATAFILETYPE
选项设置为语句的一部分。有关更多详细信息,请参阅 MSDN 文档:http://msdn。 microsoft.com/en-us/library/aa173832%28SQL.80%29.aspxIt 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不幸的是,旧的 SQL Server 版本不支持 utf-8。将代码页参数添加到批量插入方法。在您的问题中,请更改现有的代码。
请注意,您的文件必须采用 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.
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
在 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.