Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a database of servers and some servers were entered with the fully qualified domain name. I want to rename all of these. However, in some instances the server has been entered both ways. For instance, and servername. When I run my query below, these cause an error because of course that column can only contain unique records.

UPDATE ServerData
SET    ServerName = replace(ServerName, '', '')
WHERE  ServerName LIKE '';

Error "Cannot insert duplicate key row in object 'dbo.ServerData' with unique index 'UK_ServerData'."

How can I modify the query to simply ignore records that would generate an error so I can deal with them individually later?

share|improve this question
up vote 1 down vote accepted

This will check to make sure the value you are writing doesn't already exist in that field:

SET    ServerName = replace(ServerName, '', '')
FROM serverdata S
WHERE  ServerName LIKE ''
                FROM serverdata s2
                WHERE s2.servername = <> replace(s1.ServerName, '', ''))
share|improve this answer
I didn't use your answer exactly, but the "FROM serverdata S" helped fix the query I had developed. Thanks! – Devil's Advocate Jan 17 '12 at 16:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.