我想將數據從一個表複製到另一個,我得到的錯誤無法在對象'dbo.Countries'中插入重複鍵。重複的鍵值是(1)。我明白這意味着什麼,但我該如何刪除它?
這是我的SQL Server查詢:無法在對象'dbo.Countries'中插入重複鍵。重複的鍵值是(1)
SET IDENTITY_INSERT [dbo].[countries] ON
----Create TestTable
CREATE TABLE TestTable (FirstName VARCHAR(100), LastName VARCHAR(100))
----INSERT INTO TestTable using SELECT
INSERT INTO [dbo].[Countries] (countryID, countryName)
SELECT countryId, countryName
FROM [dbo].[Sheet1]
----Verify that Data in TestTable
SELECT countryID, countryName
FROM [dbo].[Countries]
----Clean Up Database
DROP TABLE TestTable
SET IDENTITY_INSERT [dbo].[countries] OFF
的錯誤信息是:
Msg 2627, Level 14, State 1, Line 6
Violation of PRIMARY KEY constraint 'PK_dbo.Countries'. Cannot insert duplicate key in object 'dbo.Countries'. The duplicate key value is (1).
The statement has been terminated.
(26 row(s) affected)
源表怎麼可能有一個重複的ID開始? –
單獨運行選擇並查看重複是否存在。 –