Query Join

Hello,

Need a suggestion on how to use the join in the following example.

Thanks in advance.
Example5.xlsx
RIASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OMC2000Commented:
select t1.Ref, t2.Code from Table1 t1 left outer join Table2 t2 on t1.Ref = t2.Ref
RIASAuthor Commented:
Thanks but , how to update the column 'Code' based on the match?
OMC2000Commented:
what do you mean "update the column 'Code' based on the match"?
You want to set value of Table1.Code to Table2.Code?
Get a highly available system for cyber protection

The Acronis SDI Appliance is a new plug-n-play solution with pre-configured Acronis Software-Defined Infrastructure software that gives service providers and enterprises ready access to a fault-tolerant system, which combines universal storage and high-performance virtualization.

RIASAuthor Commented:
yes please.
Thanks
OMC2000Commented:
UPDATE  Table2
SET     Code  = t1.Code
FROM    Table1 t1 LEFT OUTER JOIN Table2 t2
ON      t1.Ref = t2.Ref

Open in new window


But it won't help you to add missed values of Ref into Table2. You should insert missed records from Table1 into Table2

insert into Table2 (select Ref, Code from Table1 t1 where not exists (select 1 from Table2 t2 where t1.Ref = t2.Ref)

Open in new window

 
And then you actually don't need outer join

UPDATE  Table2
SET     Code  = t1.Code
FROM    Table2 t2 INNER JOIN Table1 t1
ON      t1.Ref = t2.Ref

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RIASAuthor Commented:
Perfect thanks!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.