SQL Server 2008 - 将整个文本文件批量插入到一个字段中

发布于 2024-08-24 13:04:51 字数 422 浏览 5 评论 0原文

我有一个文本文件 (txt),其中包含格式化文本(仅换行符、回车符和制表符) 它还包含德语字符。

我想使用 T-SQL 中的批量插入注释将文本文件读入数据库表中的一个字段。

我运行了这个命令:

 CREATE TABLE #MyTestTable (
    MyData NVARCHAR(MAX)
 )

 BULK INSERT [#MyTestTable]
FROM 'D:\MyTextFile.txt'

 SELECT * FROM #MyTestTable

问题是它将文本文件的每一行读入临时表中的新行。我希望它将整个文件(格式和全部)读入一行。

此外,德语字符似乎已丢失 - 在结果视图中被默认的不可打印字符替换。

有人有什么想法我可以如何实现这一目标吗?

谢谢。

I have a text file (txt) containing formatted text (just line breaks, carriage returns and tabs)
It also contains German language characters.

I want to use the Bulk Insert comment in T-SQL to read in the text file into one field within a database table.

I ran this command:

 CREATE TABLE #MyTestTable (
    MyData NVARCHAR(MAX)
 )

 BULK INSERT [#MyTestTable]
FROM 'D:\MyTextFile.txt'

 SELECT * FROM #MyTestTable

The problem is that it reads each line of the text file into a new row in the Temp table. I want it to read the whole file (formatting and all) into one row.

Also the German language characters appear to be lost - replaced by a non-printable character default in the Results View.

Anyone any ideas how I can achieve this?

Thanks.

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

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

发布评论

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

评论(3

-柠檬树下少年和吉他 2024-08-31 13:04:51

您可以使用 ROWTERMINATOR 和 CODEPAGE 参数。默认行终止符是“\r\n”。对于代码页,您需要知道原始文件的编码和数据库的默认排序规则。

BULK INSERT [#MyTestTable]
FROM 'D:\MyTextFile.txt'
WITH (ROWTERMINATOR = '\0',
      CODEPAGE = 'ACP')

另请参阅 http://msdn.microsoft.com/en-us/library/ms188365 .aspx

You can use ROWTERMINATOR and CODEPAGE parameters. Default row terminator is '\r\n'. For the CODEPAGE, you need to know encoding of your raw file and default collation of your DB.

BULK INSERT [#MyTestTable]
FROM 'D:\MyTextFile.txt'
WITH (ROWTERMINATOR = '\0',
      CODEPAGE = 'ACP')

Also see http://msdn.microsoft.com/en-us/library/ms188365.aspx

一江春梦 2024-08-31 13:04:51

使用:

FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n'

其中 | 是您的列分隔符。

Use this:

FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n'

Where | is your column delimiter.

夏尔 2024-08-31 13:04:51
  • 不要使用批量插入。每行记录一条记录。你需要编写代码。
  • 正确处理从文本文件到代码中的 unicode (nvarchar) 的转换。批量插入可能应用了标准代码页,丢失了字符。

这确实需要一些小的编程工作——一个小时左右的工作,再加上其他测试和尽可能长的运行时间。

  • don't use bulk insert. it is made to take one record per line. You need to write code.
  • Properly handle the transition from you text file to the unicode (nvarchar) in code. bulk insert probably appplied the standard codepage, loosing your characters.

This really cries for some minor programming job - an hour work or so, plus naother testing and as long for running as it takes.

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