sql-server - 在 SQL Server 中加载大文本文件

标签 sql-server flat-file

我想加载一个非常大的文件 3GB 文本(不是逗号分隔)只是一个文本,因此文本中的每一行都成为一条记录

我的表结构应该是这样的

加载表 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 )

您可以运行 BULK INSERT 查询,而不是使用 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

但是,您之前需要做一些事情:

  1. 创建 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 将信息保存到新表中,使用 INSERT 将新行添加到现有表中,甚至可以使用 UPDATE 将列更新到现有表中。

  • 就我而言,MyTable 的内容如下所示:

    enter image description here


    创建格式文件的一个简单解决方法是在数据库中创建一个表,其结构是您期望输入数据具有的结构:

    1. 使用结构/定义创建虚拟表(例如:big_bulk)

    create table big_bulk (txt varchar(max))

  • 运行 BCP 命令以从此表中生成文件格式:
  • 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 文件并确保设置正确的行终止符/分隔符。

  • OPENROWSET() 查询中使用文件格式。

  • 关于sql-server - 在 SQL Server 中加载大文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58971358/

    相关文章:

    csv - BizTalk 平面文件架构 - 即使收到空白平面文件也生成 XML 文件

    php - 将 php 文件中的代码从 mysql 更改为 mssql

    sql-server - 如何在 Azure DevOps 管道中将变量注入(inject) SQL 脚本?

    sql-server - SELECT 语句中的 SQL WHERE 语句

    database-design - 平面文件数据库示例

    python - Python中根据子字符串删除重复项

    sql - 根据行的组合返回结果

    sql - 在单个查询中使用 "Order by"两次

    sql-server - SSIS 参差不齐的文件无法识别 CRLF

    SQL Developer 假脱机 : how to get rid of the first, 空行