Heather Won
Heather Won

Reputation: 23

SQL select a column and add

I have two tables RBL and region.

Select *, r.RegionId
from RBL b
left join region r 
on r.Name = b.Region_name

After left join, I want to add the RegionId column into RBL.

Upvotes: 0

Views: 36

Answers (2)

Lamak
Lamak

Reputation: 70648

Since you already have an empty column, you need to update its value:

UPDATE A
SET A.RegionId = B.RegionId
FROM RBL A
INNER JOIN Region B
   ON A.Region_name = B.Name;

Upvotes: 0

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48197

First you update your table to include a new field RegionId

ALTER TABLE

Then update the table

UPDATE RBL 
SET RegionId =  (SELECT  r.RegionId
                 FROM region r 
                 WHERE r.Name = RBL.Region_name)

Upvotes: 1

Related Questions