when column C1 is the joining-column between tableA and table B
Main Topics
Browse All TopicsI need to write a stored procedure that goes through each record in Table A and checks if it is present in Table B. If it is present update the record in Table B, if not present, insert the record into Table B. I'm using MS SQL 2000.
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
the solllution fo aneeshattingal will only work if you want to update 1 specific record
When you execute it in a loop record per record it will be very much slower as the first two.
and now I see that I forgot a little thing in the WHERE
-- Update the existing (and don't update if the result already has the right value (pretending there are no NULL-values in either column)
Update tableB
set TableB.c2 = TableA.C2
from tableB
inner join tableA on tableA.C1 = tableB.C1
where TableB.c2 <> TableA.C2
Business Accounts
Answer for Membership
by: chapmandewPosted on 2008-04-17 at 09:27:00ID: 21378839
this should do it..you have to update then insert..2 statements.
create proc myproc
as
begin
update b
set fieldname = a.fieldname
from tablea a
join tableb b on a.pkey = b.pkey
insert into tableb
select a.*
from tablea a
left join tableb b on a.pkey = b.pkey
where b.pkey is null
end