Quantcast
Viewing all articles
Browse latest Browse all 23857

How does this UPDATE update multiple rows?

For the UPDATE in the following script, what values are selected to update Table2? For example, for a row in Table2 (ie. 'NY') there are several rows in Table1 that can be used for the update.

Thanks.

create table Table1
(
	Col1 varchar(10),
	Col2 varchar(10),
	Col3 varchar(10)
)
Insert into Table1
select 'FL', 2, 2
union
select 'FL', 2, 2
union
select 'FL', 2, 5
union
select 'FL', 2, 3
union
select 'NY', 2, 2
union
select 'NY', 1, 3
union
select 'NY', 2, 2
union
select 'NY', 2, 3
union
select 'CA', 3, 2
union
select 'CA', 5, 1
union
select 'CA', 4, 2
union
select 'CA', 2, 3

create table Table2
(
	Col1 varchar(10),
	Col2 varchar(10),
	Col3 varchar(10),
	Ignore varchar(10)
)
Insert into Table2 (Col1, Ignore)
select 'FL', 1
union
select 'FL', 2
union
select 'FL', 3
union
select 'FL', 4
union
select 'NY', 5
union
select 'NY', 6
union
select 'NY', 7
union
select 'NY', 8
union
select 'CA', 9
union
select 'CA', 10
union
select 'CA', 11
union
select 'CA', 12

update T2
set Col2 = T1.Col2,
Col3 = T1.Col3
from Table1 T1 inner join Table2 T2
on T1.Col1 = T2.Col1


select col1, col2, col3 from Table1
select col1, col2, col3 from table2


VM


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>