Question

I’m doing:

SELECT uid, intStatus from dbo.MYTABLE 
WHERE 
intStatus = 10 and uid = 'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1'

And I get:

        uid                            |  intStatus 
-------------------------------------- +---------------
'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1  | 10

Then I need to do an update, I need the intStatus to be 18 for that uid. So I do:

UPDATE dbo.MYTABLE  SET intStatus = 18 
WHERE 
intStatus = 10 and uid = 'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1'

And here’s where I’m getting the error:

Cannot insert duplicate key row in object 'dbo.MYTABLE' with unique index 'ixMYTABLE'.

Can anyone, please, tell me why I’m getting this error? How can I solve it?

uid is primary key for MYTABLE

intStatus is a not null int

Thanks a ton!

Était-ce utile?

La solution

Probably you have already a row with intStatus 18 and uid 'a1a1...'

Try to run and see if you have already the row that you try to create with your UPDATE:

SELECT uid, 
       intStatus 
  FROM dbo.MYTABLE 
 WHERE intStatus = 18 
   AND uid = 'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1'

Autres conseils

If uid is your primary key then it is unique by definition, therefore the intStatus condition is irrelevant. This should work:

UPDATE dbo.MYTABLE  SET intStatus = 18 
WHERE  uid = 'a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1';

If that doesn't work, then you could have a composite key combining both columns rather than a single column key.

Info on primary keys and composites

intStatus is unique thatswhy mysql throws an error.

if u want to execute that query intStatus should not be unique

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