SQL SERVER RULES

SQL SERVER RULES

Sunday, February 12, 2012

Pushing the data of 2 tables into 1 new table

hi Guys,

Basically there are two ways to solve the scenario

1st solution: By using the FULL OUTER JOIN

Example:
insert into table3 select t1.col1,t1,col2,t2.col3 from table1 as t1
full outer join
table2 as t2
on t1.col1 = t2.col1

2nd colution: By using the MERGE STATEMENT

EXAMPLE:

merge tab3 as t3
using
(
select t1.col1,col2,col3 from tab1 as t1
join tab2 as t2
on t1.col1 = t2.col1
) as t4
on t4.col1 = t3.col1
when not matched then
insert(col1,col2,col3)
values(t4.col1,t4.col2,t4.col3);

No comments: