sam it
sam it

Reputation: 54

Tsql Update Table with other table

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

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

Answers (2)

maulik kansara
maulik kansara

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

SQL_M
SQL_M

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

Related Questions