SQL BULK INSERT FROM 错误

发布于 2024-10-01 21:38:16 字数 1922 浏览 7 评论 0原文

我正在尝试将 CSV 文件插入到 Microsoft SQL Server Management Studio 数据库中,如下所示:

    BULK INSERT [dbo].[STUDY]
        FROM 'C:\Documents and Settings\Adam\My Documents\SQL Server Management Studio\Projects\StudyTable.csv' 
    WITH 
    ( 
        MAXERRORS = 0,
        FIELDTERMINATOR = ',',
        ROWTERMINATOR = '\n'
    )

但我收到错误:

    Msg 4863, Level 16, State 1, Line 2
    Bulk load data conversion error (truncation) for row 1, column 9 (STATUS).

    Msg 7399, Level 16, State 1, Line 2
    The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.

    Msg 7330, Level 16, State 2, Line 2
    Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".

不幸的是,我无法提供 StudyTable.csv 的内容,以保护我们工作的公司。

编辑不过,我可以保证csv文件的有效性。它是从 OpenOffice 版本的 Excel 导出的,我检查并确保它是有效的。

编辑2 这是 CSV 文件的虚拟版本:

    1234,,,1234,1234,,"asdf","asdf","Z","asd",7/1/2010 12:23,8/5/2010 13:36,9/4/2010 13:36,"(asdf,1661,#1234,F,T)","F",,,"F",,"68866",1234,1234,1234,"F"

这是 STUDY 表的创建脚本:

CREATE TABLE [dbo].[STUDY]
(
    [STUDY_ID] INT IDENTITY(1,1) NOT NULL,
    [PARENT_ID] INT,
    [GROUP_ID] INT,
    [WORKFLOW_NODE_ID] INT,
    [STUDY_TEMPLATE_ID] INT,
    [INSPECTION_PLAN_ID] INT,
    [NAME] VARCHAR(255),
    [DESCRIPTION] VARCHAR(4000),
    [STATUS] VARCHAR,
    [OLD_STATUS] VARCHAR,
    [CREATED_ON] DATE,
    [COMPLETED_ON] DATE,
    [AUTHORIZED_ON] DATE,
    [EVENTS] VARCHAR,
    [NEEDS_REVIEW] CHAR,
    [HAS_NOTES] CHAR,
    [HAS_AUDITS] CHAR,
    [STUDY_PART] CHAR,
    [STUDY_TYPE] VARCHAR,
    [EXTERNAL_REFERENCE] VARCHAR,
    [CREATED_BY] INT,
    [COMPLETED_BY] INT,
    [AUTHORISED_BY] INT,
    [ARCHIVED_CHILD_COMPLETE] CHAR
)

I'm attempting to insert a CSV file into an Microsoft SQL Server Management Studio database like this:

    BULK INSERT [dbo].[STUDY]
        FROM 'C:\Documents and Settings\Adam\My Documents\SQL Server Management Studio\Projects\StudyTable.csv' 
    WITH 
    ( 
        MAXERRORS = 0,
        FIELDTERMINATOR = ',',
        ROWTERMINATOR = '\n'
    )

But I am getting errors:

    Msg 4863, Level 16, State 1, Line 2
    Bulk load data conversion error (truncation) for row 1, column 9 (STATUS).

    Msg 7399, Level 16, State 1, Line 2
    The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.

    Msg 7330, Level 16, State 2, Line 2
    Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".

Unfortunately, I cannot provide the contents of StudyTable.csv, to protect the privacy of the company we're working for.

EDIT I can vouch for the validity of the csv file though. It was exported from OpenOffice's version of Excel, and I went through and made sure it was valid.

EDIT2
Here's a dummy version of the CSV file:

    1234,,,1234,1234,,"asdf","asdf","Z","asd",7/1/2010 12:23,8/5/2010 13:36,9/4/2010 13:36,"(asdf,1661,#1234,F,T)","F",,,"F",,"68866",1234,1234,1234,"F"

Here's a create script for the STUDY table:

CREATE TABLE [dbo].[STUDY]
(
    [STUDY_ID] INT IDENTITY(1,1) NOT NULL,
    [PARENT_ID] INT,
    [GROUP_ID] INT,
    [WORKFLOW_NODE_ID] INT,
    [STUDY_TEMPLATE_ID] INT,
    [INSPECTION_PLAN_ID] INT,
    [NAME] VARCHAR(255),
    [DESCRIPTION] VARCHAR(4000),
    [STATUS] VARCHAR,
    [OLD_STATUS] VARCHAR,
    [CREATED_ON] DATE,
    [COMPLETED_ON] DATE,
    [AUTHORIZED_ON] DATE,
    [EVENTS] VARCHAR,
    [NEEDS_REVIEW] CHAR,
    [HAS_NOTES] CHAR,
    [HAS_AUDITS] CHAR,
    [STUDY_PART] CHAR,
    [STUDY_TYPE] VARCHAR,
    [EXTERNAL_REFERENCE] VARCHAR,
    [CREATED_BY] INT,
    [COMPLETED_BY] INT,
    [AUTHORISED_BY] INT,
    [ARCHIVED_CHILD_COMPLETE] CHAR
)

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

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

发布评论

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

评论(2

如梦 2024-10-08 21:38:16

听起来 .csv 文件中的 STATUS 列中的数据比 SQL 表中字段的定义长。

检查该字段的定义并确保 .csv 中的数据适合(长度和类型)。

It sounds like the data in your STATUS column in the .csv file is longer than the definition for the field in your SQL Table.

Check the definition on that field and make sure the data you have in the .csv will fit (both length and type).

驱逐舰岛风号 2024-10-08 21:38:16

第一个错误可能是因为列太大而无法容纳数据库。首先想到的是 CSV 文件是否包含列标题?

第二个错误非常普遍。

您的任何字段是否包含逗号作为其数据的一部分 - 例如未用引号引起来的 37, High Street?过去,由于数据中的“问题”,我曾多次陷入困境(数据是有效的,只是不完全符合预期)。如果您的 CSV 文件有几千行或更多 - 如果您尝试仅导入前一百(或十)行会发生什么?

The first error is likely because a column is too big to fit the database. First thought is does the CSV file contain column headers?

The second error is very generic.

Do any of your fields contain a comma as part of their data - for example 37, High Street which isn't encased in quotes? I've come unstuck lots of times in the past because of "issues" in the data (the data is valid, just not quite what's expected). If your CSV file is a few thousand rows or more - what happens if you try importing just the first hundred (or ten) rows?

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