Click here to Skip to main content
15,918,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to write a query to match a col "bus_name" in two table and if it matches copy the revenue from table 2 to table1.
Table 1 : (-- means space)
Bus_id--bus_name--Bus_address--revenue
1010--Tata motr--Coll north-- NULL
1020--Celeriac--Celeriac new-- NULL
1030--Tagore--Tagore south-- NULL
1040--Nehru--Nehru King-- NULL

Table 2 :
New_id--bus_name--Bus_address--revenue
20--Class new--westgate new-- 75050
10--Tata motr--north-- 10050
30--Glass--Glass-- 80900
40--Tagore--Tagore south-- 25000

Resulting table t1 should be:
Bus_id--bus_name--Bus_address--revenue
1010--Tata motr--Coll north-- 10050
1020--Celeriac--Celeriac new-- NULL
1030--Tagore--Tagore south-- 25000
1040--Nehru--Nehru King-- NULL

I have written query as below but it doesnt work :

Update t1
set t1.Revenue = t2.revenue
when (
select * from t1
Inner Join t2
on t1.r1_bus_name_full = t2.r1_bus_name_full ) ;

Pls help !!
Posted

1 solution

Please change your query like this and try:
C#
update a set a.revenue = b.revenue
from table1 a inner join table2 b 
on a.busname = b.busname

You can also use MERGE to achieve the same:
C#
merge table1 as a
using table2 as b
on (a.busname = b.busname)
when matched
	then update set a.revenue = b.revenue;


Note: Column names may not match, but you get the idea.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900