Question

I’m doing:

DELETE FROM mytable WHERE id = '6B4AE2C6-7C26-4BB0-AEC2-9590A7A167F3' AND idFK='F3696D77-AC4A-4235-B6CB-C887D62FF860' AND data=3

(Id is a PK uniqueidentifier and idFK is a FK uniquedientifier, not null both. Data is int not null)

Then I do:

INSERT INTO mytable (id, idFK, data) 
VALUES ('6B4AE2C6-7C26-4BB0-AEC2-9590A7A167F3','F3696D77-AC4A-4235-B6CB-887D62FF860',6)

And here’s where I get the error:

Cannot insert duplicate key row in object 'dbo.mytable' with unique index 'ixmytable'.

Here’s the select:

SELECT * FROM mytable WHERE id = '6B4AE2C6-7C26-4BB0-AEC2-9590A7A167F3' AND idFK='F3696D77-AC4A-4235-B6CB-C887D62FF860' AND data=3

And what it shows:

------------------+ id +--                             ---+ idFK +------   -+ data 
6B4AE2C6-7C26-4BB0-AEC2-9590A7A167F3    F3696D77-AC4A-4235-B6CB-C887D62FF860    3

Why am I getting this error?

Était-ce utile?

La solution

If you want to delete a row from your table then for deletion only

DELETE FROM mytable WHERE id = '6B4AE2C6-7C26-4BB0-AEC2-9590A7A167F3'

is enough. But if you want to update particular value then you can try with

UPDATE mytable SET DATA = 6 WHERE id = '6B4AE2C6-7C26-4BB0-AEC2-9590A7A167F3'

Because id is an unique identifier, and it can be used to uniquely identify each row. And I think originally in your database the value of idFK column is having one space, so you are getting error.

//Third Query
idFK=' F3696D77-AC4A-4235-B6CB-C887D62FF860'
      ^
//But First and second query
idFK='F3696D77-AC4A-4235-B6CB-C887D62FF860'

Autres conseils

try to do this before insert :

SET IDENTITY_INSERT ON
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top