Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 only do update if there is not a unique key constr

Author  Topic 

MyronCope
Starting Member

46 Posts

Posted - 2008-01-15 : 12:59:59
Question: i have to do an update on a table that has a nested index of three fields. however i'm only getting one field as a parameter to work with and i am getting a constraint because there cannot be duplicate records.

Is there a way to only do the update code if the unique key constraint is not thrown? i was thinking about doing this in an if statement but the problem is i do not have all three keys that i can use to do the right query. thanks

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-01-15 : 18:40:03
I am assuming you mean primary key instead of index?

You would update something like this I am guessing..

Update [Table]
Set [Column] = @Parameter
Where @parameter not in (Select [Column] FROM [Table])




Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-15 : 22:51:23
or:-
Update 
Set [Column] = @Parameter
FROM [Table] t1
LEFT OUTER JOIN
(Select distinct [Column] FROM [Table] WHERE [Column]=@Parameter)t2
ON t2.[Column]=t1.[Column]
WHERE t2.[Column] IS NULL

Go to Top of Page
   

- Advertisement -