2014-07-15 141 views
0

使用SQL Server 2008無法插入一排表

customerinfo腳本

CREATE TABLE [dbo].[customerinfo](
    [customer#] [int] IDENTITY(1,1) NOT NULL, 
    [acc#] [varchar](10) NOT NULL, 
    CONSTRAINT [PK_unc_customerinfo] PRIMARY KEY NONCLUSTERED 
(
    [customer#] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], 
CONSTRAINT [idx_u_customerinfo] UNIQUE NONCLUSTERED 
(
    [acct#] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

創建表重置身份場補種0,然後我嘗試在表中插入行。獲取錯誤爲

Violation of UNIQUE KEY constraint 'idx_u_customerinfo'. Cannot insert duplicate key in object 'dbo.customerinfo'. The duplicate key value is (50950/01950). 

如何糾正此問題。

+0

你想喲能夠在列'ACC#添加重複值'? –

+0

另外,您要添加的值看起來像11個字符,而列僅爲10個字符。也許你正在獲取重複的值,因爲最後一個字符被切斷。 –

+0

[customer#]是主鍵...它不允許重複值 – Jesuraja

回答

0

第一:您的查詢是錯誤的。

idx_u_customerinfoacc#acct#上的唯一非聚集索引。 (acct#不是你的表的一個字段)。

第二:檢查插入的數據,當然你有多個行相同acc#

如果使用Insert Into [dbo].[customerinfo] Select ...查詢插入數據到表,下面的查詢,以便找到重複記錄使用:

Select acc#, count(*) 
from .. 
group by acc# 
having count(*)<>1