November 2, 2011 at 5:57 am
Hi all,
i am trying to execute this query:
begin tran
insert into Attributes (Id, EntityId, AttributeName, IgnoreOriginal,
OriginalValue, NewValue)
select Id, EntityId, AttributeName, IgnoreOriginal,
OriginalValue, NewValue
from MOHG_ProcessedData_01112011.dbo.Attributes
but am getting this error:
Msg 2627, Level 14, State 1, Line 2
Violation of PRIMARY KEY constraint 'PK_Attributes'. Cannot insert duplicate key in object 'dbo.Attributes'. The duplicate key value is (1129614).
any help is appreciated
November 2, 2011 at 6:00 am
you have a primary key and are trying to insert a duplicate.
search your source table for id =1129614 and it should have more than one row.
November 2, 2011 at 6:03 am
yes this is correct; so how do i make the id sequential starting from 1000000? so i do not have any duplicates
November 2, 2011 at 6:08 am
i used this code to do this:
DECLARE @SourceID INT;
DECLARE @Counter INT;
SET @Counter = 1;
DECLARE ins_accounts CURSOR
FOR
SELECT id FROM attributes
OPEN ins_accounts
FETCH NEXT FROM ins_accounts
INTO @SourceId
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE attributes
SET id = 1000000 + CAST(@Counter AS int)
WHERE id = @SourceID
FETCH NEXT FROM ins_accounts INTO @SourceId
SET @Counter = @Counter+1
END
CLOSE ins_accounts
DEALLOCATE ins_accounts
November 2, 2011 at 6:08 am
since you are migrating from another table, and at least one value already exists, shouldn't you be joiing on the local data to prevent the duplicates from being inserted?
or do you really want to re-insert the data but with new ID's? is the table 's [ID] column an identity?
something like this?
INSERT INTO Attributes
(Id,
EntityId,
AttributeName,
IgnoreOriginal,
OriginalValue,
NewValue)
SELECT RemoteSource.Id,
RemoteSource.EntityId,
RemoteSource.AttributeName,
RemoteSource.IgnoreOriginal,
RemoteSource.OriginalValue,
RemoteSource.NewValue
FROM MOHG_ProcessedData_01112011.dbo.Attributes RemoteSource
LEFT OUTER JOIN Attributes LocalSource
ON RemoteSource.ID = LocalSource.ID
WHERE LocalSource.ID IS NULL --not yet migrated.
Lowell
November 2, 2011 at 6:10 am
please see my posts above, i do not want duplicates but need to set the id sequential starting from 1000000
November 2, 2011 at 6:22 am
so the id is meaningless?
if so you can use
DBCC CHECKIDENT ("Attributes ", RESEED, 10000);
to reseed you id column
November 2, 2011 at 6:25 am
yes pretty much meaningless, what does that statement actually do though?
'id' does mean something but i have 2 tables with results in that i want to amalgamate but they both have the same sequential 'id'
November 2, 2011 at 6:40 am
it starts the id at 10000
January 17, 2012 at 8:46 am
Hi
Try this it will make you insert from other table and id starting with 1000000
DECLARE @START INT
DECLARE @END INT
SELECT @START = 1000000 --START VALUE
SET @END = (SELECT COUNT(*) FROM Attributes)
SELECT @END + 1000000 --END VALUE
WHILE @START <= @END --CONDITION WHERE STARTVALUE SHOULD BE LESS THEN END VALUE
BEGIN
insert into Attributes (Id, EntityId, AttributeName, IgnoreOriginal,
OriginalValue, NewValue)
select @START, EntityId, AttributeName, IgnoreOriginal,OriginalValue, NewValue
from MOHG_ProcessedData_01112011.dbo.Attributes
SET @START = @START + 1 --INCREMENTING THE START VALUE
END
Thanks & Regards
Syed Sami Ur Rehman
SQL-Server (Developer)
Hyderabad
Email-sami.sqldba@gmail.com
January 17, 2012 at 9:10 am
sami.sqldba (1/17/2012)
HiTry this it will make you insert from other table and id starting with 1000000
DECLARE @START INT
DECLARE @END INT
SELECT @START = 1000000 --START VALUE
SET @END = (SELECT COUNT(*) FROM Attributes)
SELECT @END + 1000000 --END VALUE
WHILE @START <= @END --CONDITION WHERE STARTVALUE SHOULD BE LESS THEN END VALUE
BEGIN
insert into Attributes (Id, EntityId, AttributeName, IgnoreOriginal,
OriginalValue, NewValue)
select @START, EntityId, AttributeName, IgnoreOriginal,OriginalValue, NewValue
from MOHG_ProcessedData_01112011.dbo.Attributes
SET @START = @START + 1 --INCREMENTING THE START VALUE
END
This will always fail, since every row to be inserted into the attributes table (in the only insert to run) will have the same Id: 1000000
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
January 18, 2012 at 4:18 am
martin.kerr 34088 (11/2/2011)
i used this code to do this:...
UPDATE attributes
SET id = 1000000 + CAST(@Counter AS int)
WHERE id = @SourceID
FETCH NEXT FROM ins_accounts INTO @SourceId
SET @Counter = @Counter+1
...
Looks like you are just resequencing the ids, if so, try it like this:
UPDATE Identifiers
SET Id = Sequence
FROM
(
SELECT
Id,
Sequence =
1000000 +
ROW_NUMBER() OVER (
ORDER BY Id)
FROM MOHG_ProcessedData_01112011.dbo.Attributes
) AS Identfiers;
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply