在 SQL 服务器中加载大文本文件
Load big text file in SQL Server
我想加载一个非常大的文件 3GB 的文本(不是逗号分隔)只是一个文本,所以文本中的每一行都成为一条记录
我的table结构应该是这样的
加载表
ID bigint身份
TLine varchar(max)
我尝试使用 SQL 任务导入数据,但总是出现此错误
- Executing (Error) Messages Error 0xc02020a1: Data Flow Task 1: Data conversion failed. The data conversion for column "Column 0" returned
status value 4 and status text "Text was truncated or one or more
characters had no match in the target code page.". (SQL Server Import
and Export Wizard)
Error 0xc020902a: Data Flow Task 1: The "Source -
NOTEEVENTS_csv.Outputs[Flat File Source Output].Columns[Column 0]"
failed because truncation occurred, and the truncation row disposition
on "Source - NOTEEVENTS_csv.Outputs[Flat File Source
Output].Columns[Column 0]" specifies failure on truncation. A
truncation error occurred on the specified object of the specified
component. (SQL Server Import and Export Wizard)
Error 0xc0202092: Data Flow Task 1: An error occurred while processing
file "E:\MyFile.txt" on
data row 1. (SQL Server Import and Export Wizard)
Error 0xc0047038: Data Flow Task 1: SSIS Error Code
DTS_E_PRIMEOUTPUTFAILED. The PrimeOutput method on Source -
NOTEEVENTS_csv returned error code 0xC0202092. The component returned
a failure code when the pipeline engine called PrimeOutput(). The
meaning of the failure code is defined by the component, but the error
is fatal and the pipeline stopped executing. There may be error
messages posted before this with more information about the failure.
(SQL Server Import and Export Wizard)
如何修复此错误并将每行加载为 ID 显示行顺序的记录?
VARCHAR(max)
列(行)的最大存储大小为 2Gb,as per the documentation:
varchar [ ( n | max ) ] Variable-size string data. Use n to define the
string size in bytes and can be a value from 1 through 8,000 or use
max to indicate a column constraint size up to a maximum storage of
2^31-1 bytes (2 GB).
(出于演示目的,我使用了 big.txt file from Peter Norvig's site)
您可以 运行 批量插入查询,而不是使用 SSMS 中的用户界面,如下所示:
SELECT
ROW_NUMBER() over (ORDER BY (SELECT NULL)) ROW_NR
, *
INTO MyTable
FROM OPENROWSET
(BULK N'C:\..\Desktop\big.txt', FORMATFILE=N'C:\..\Desktop\big_format_file.xml') tmp
不过,您需要先做几件事:
创建一个 Format File,以指定格式(就像您为平面文件导入指定行终止符、列终止符一样)。 (在我的答案底部生成此文件的简单解决方法)
我选择创建 .XML 文件格式,因为它更易于阅读。这个文件的内容是:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\n" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="txt" xsi:type="SQLVARYCHAR"/>
</ROW>
</BCPFORMAT>
虽然这是一个示例文档,但请确保 TERMINATOR 选项设置了您希望作为行分隔符的正确终止符。
之后,您可以运行上面的查询将您的数据导入MyTable
。
从现在开始,您可以使用 SELECT INTO 将信息保存到新的 table 中,使用 INSERT 将新行添加到现有的 table 中,甚至可以更新到将列更新为现有的 table.
在我的例子中,MyTable
的内容将如下所示:
创建格式文件的一个简单解决方法是在数据库中创建一个table,您希望输入数据具有的结构是:
- 使用结构/定义创建虚拟 table(例如:big_bulk)
create table big_bulk (txt varchar(max))
- 运行 BCP 命令由此生成文件格式 table:
bcp test.dbo.big_bulk format nul -c -x -f .\Desktop\big_format_file.xml -t, -T
The -t parameter in the bcp command above specifies the row delimiter. You can replace the comma ( "," ) with a "\t" or "\r" or other Field and Row Terminators
编辑 XML 文件并确保设置正确的行 terminator/delimiter.
在您的 OPENROWSET()
查询中使用文件格式。
我想加载一个非常大的文件 3GB 的文本(不是逗号分隔)只是一个文本,所以文本中的每一行都成为一条记录
我的table结构应该是这样的
加载表 ID bigint身份 TLine varchar(max)
我尝试使用 SQL 任务导入数据,但总是出现此错误
- Executing (Error) Messages Error 0xc02020a1: Data Flow Task 1: Data conversion failed. The data conversion for column "Column 0" returned status value 4 and status text "Text was truncated or one or more characters had no match in the target code page.". (SQL Server Import and Export Wizard)
Error 0xc020902a: Data Flow Task 1: The "Source - NOTEEVENTS_csv.Outputs[Flat File Source Output].Columns[Column 0]" failed because truncation occurred, and the truncation row disposition on "Source - NOTEEVENTS_csv.Outputs[Flat File Source Output].Columns[Column 0]" specifies failure on truncation. A truncation error occurred on the specified object of the specified component. (SQL Server Import and Export Wizard)
Error 0xc0202092: Data Flow Task 1: An error occurred while processing file "E:\MyFile.txt" on data row 1. (SQL Server Import and Export Wizard)
Error 0xc0047038: Data Flow Task 1: SSIS Error Code DTS_E_PRIMEOUTPUTFAILED. The PrimeOutput method on Source - NOTEEVENTS_csv returned error code 0xC0202092. The component returned a failure code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing. There may be error messages posted before this with more information about the failure. (SQL Server Import and Export Wizard)
如何修复此错误并将每行加载为 ID 显示行顺序的记录?
VARCHAR(max)
列(行)的最大存储大小为 2Gb,as per the documentation:
varchar [ ( n | max ) ] Variable-size string data. Use n to define the string size in bytes and can be a value from 1 through 8,000 or use max to indicate a column constraint size up to a maximum storage of 2^31-1 bytes (2 GB).
(出于演示目的,我使用了 big.txt file from Peter Norvig's site)
您可以 运行 批量插入查询,而不是使用 SSMS 中的用户界面,如下所示:
SELECT
ROW_NUMBER() over (ORDER BY (SELECT NULL)) ROW_NR
, *
INTO MyTable
FROM OPENROWSET
(BULK N'C:\..\Desktop\big.txt', FORMATFILE=N'C:\..\Desktop\big_format_file.xml') tmp
不过,您需要先做几件事:
创建一个 Format File,以指定格式(就像您为平面文件导入指定行终止符、列终止符一样)。 (在我的答案底部生成此文件的简单解决方法)
我选择创建 .XML 文件格式,因为它更易于阅读。这个文件的内容是:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\n" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="txt" xsi:type="SQLVARYCHAR"/>
</ROW>
</BCPFORMAT>
虽然这是一个示例文档,但请确保 TERMINATOR 选项设置了您希望作为行分隔符的正确终止符。
之后,您可以运行上面的查询将您的数据导入
MyTable
。从现在开始,您可以使用 SELECT INTO 将信息保存到新的 table 中,使用 INSERT 将新行添加到现有的 table 中,甚至可以更新到将列更新为现有的 table.
在我的例子中,MyTable
的内容将如下所示:
创建格式文件的一个简单解决方法是在数据库中创建一个table,您希望输入数据具有的结构是:
- 使用结构/定义创建虚拟 table(例如:big_bulk)
create table big_bulk (txt varchar(max))
- 运行 BCP 命令由此生成文件格式 table:
bcp test.dbo.big_bulk format nul -c -x -f .\Desktop\big_format_file.xml -t, -T
The -t parameter in the bcp command above specifies the row delimiter. You can replace the comma ( "," ) with a "\t" or "\r" or other Field and Row Terminators
编辑 XML 文件并确保设置正确的行 terminator/delimiter.
在您的
OPENROWSET()
查询中使用文件格式。