Reputation: 54
I have two table sql
I want to update table A with data from table
my issu is that
if (element exist update else update)
table a
table b
clubid | memeber
1 |200
I want table a will update like :
at the end
table a will be like that
I can t figure out how to do it can you please help me
Upvotes: 0
Views: 60
Reputation: 1107
you can use Merge
.
merge tableA trg
using tableB src on trg.clubid=src.clubid
when matched then
update set trg.member=src.member
when not matched by trg then
insert(clubid,member)
values (src.clubid,src.member);
Upvotes: 0
Reputation: 2475
Two separate statements would do it:
UPDATE TableA SET TableA.Member = TableB.Member
FROM TableB
WHERE TableA.ClubID = TableB.ClubID
INSERT INTO TableA
SELECT * FROM TableB WHERE ClubID NOT IN ( SELECT ClubID FROM TableA )
Upvotes: 1