今天匯入一個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將有問題的資料刪除,沒什麼研究精神,但很符合這個趕得要命專案的需求。


Comments

# by freemans

Agree! you are right. MS is nonsense.

# by Dua

谢谢,帮了大忙了

# by jerryhsiao

沒錯,符合趕得要命專案的需求

# by 阿多

外國人也從這篇文章解決問題了,太強! http://doogalbellend.blogspot.com/2009/01/value-violated-integrity-constraints.html

Post a comment