SQL BULK INSERT FROM errors

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 06:55:16

问题


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
)

回答1:


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).




回答2:


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?



来源:https://stackoverflow.com/questions/4134603/sql-bulk-insert-from-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!