Importing Excel To SQL: The value violated the integrity constraints

今天匯入一個Excel 工作表到SQL 2005時,為了省空間,我沒依它建議,將每個欄位都設成nchar(255),而是一一決定用nvarchar, varchar或char,指定適當的長度,有些非NULL的欄位也一併加上不允許NULL的限制以求嚴謹。

開始匯入後,我收到以下的錯誤訊息:

Messages
Error 0xc0202009: Data Flow Task: An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80004005 Description: "Unspecified error".
(SQL Server Import and Export Wizard)
Error 0xc020901c: Data Flow Task: There was an error with input column "Company" (52) on input "Destination Input" (42). The column status returned was: "The value violated the integrity constraints for the column.".
(SQL Server Import and Export Wizard)
Error 0xc0209029: Data Flow Task: The "input "Destination Input" (42)" failed because error code 0xC020907D occurred, and the error row disposition on "input "Destination Input" (42)" specifies failure on error. An error occurred on the specified object of the specified component.
(SQL Server Import and Export Wizard)
Error 0xc0047022: Data Flow Task: The ProcessInput method on component "Destination - Constituents$" (29) failed with error code 0xC0209029. The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.
(SQL Server Import and Export Wizard)
Error 0xc0047021: Data Flow Task: Thread "WorkThread0" has exited with error code 0xC0209029.
(SQL Server Import and Export Wizard)

不符合Column Integrity? 我第一個想到是,我沒有設任何Primary Key,應該沒有重覆的問題才對! 挖了一陣子才發現,原來是SQL 2005在匯入時,錯誤地將工作表中最後一列全空的儲存格當成有值,所以INSERT了一筆所有欄位都是NULL的資料,而這違背我所設的NOT NULL限制。

最後我採行的解決方法,不是去研究為什麼Excel中的空格會被當成有值,而是先取消NOT NULL的限制,待資料匯入後,再用DELETE FROM myTable WHERE Col1 IS NULL將有問題的資料刪除,沒什麼研究精神,但很符合這個趕得要命專案的需求。

Published 01 June 2007 08:40 PM 由 Jeffrey


意見

# freemans said on 02 December, 2007 09:41 PM

Agree! you are right. MS is nonsense.

# Dua said on 03 June, 2008 05:32 AM

谢谢,帮了大忙了

你的看法呢?

(必要的) 
(必要的) 
(選擇性的)
(必要的) 

請輸入以上的數字:

【請注意】意見送出後可能需要幾分鐘才會出現在網頁上,請耐心等候。

搜尋

Go

<June 2007>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
 
RSS
最新回應


BlogLook Score and Rank

Syndication