What you're looking for is a Merge, for the insert/update. Here is an example that should help you:
Create table #Test (CustomerID int,
Name varchar(20),
City varchar(20));
Insert into #Test
Values (1, 'Steve','London');
Select *
from #Test;
MERGE #Test as Target
Using (Select 1, 'Steve', 'Colchester') as source (CustomerID, Name, City)
On (Target.CustomerID = source.CustomerID)
when matched then
update SET Name = source.name,
city = source.city
when not matched then
Insert ( CustomerID, Name, City)
Values (source.CustomerID, source.Name, Source.City);
Select *
from #test;
MERGE #Test as target
Using (Select 2, 'Donna', 'Leeds') as source (CustomerID, Name, City)
On (Target.CustomerID = source.CustomerID)
when matched then
update SET Name = source.name,
city = source.city
when not matched then
Insert ( CustomerID, Name, City)
Values (source.CustomerID, source.Name, Source.City);
Select *
from #Test;
Drop table #Test;
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk