I have two tables Table1(Name,Size,qty) and Table2(Name,Size,qty)
I want to check Name and Size is equal to or not. If name and size of Table1 equals to Name and Size of Table2 then select row from Table2. Otherwise Select from Table1.
Select rows from two tables
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.


Khan Abrar AhmedRank #778 · 3K reputationPosted Jun 18, 2014, 5:56 AM EDT
declare @Table1 as table (Name varchar(64),Size int ,qty int)
declare @Table2 as table (Name varchar(64),Size int ,qty int,t int)
insert into @Table1 values ('ABC',1,2), ('CD',2,3)
insert into @Table2 values ('ABC',1,2,2), ('JF',4,5,2)
select t2.* from @table2 t2
inner join (
select Name,size from @table2
intersect
select Name,size from @table1
) a on a.Name =t2.name and a.size=t2.size
union
select t1.*,null from @table1 t1
inner join (
select Name,size from @table1
except
select Name,size from @table2
) b on b.Name =t1.name and b.size=t1.size
Abhishek JaiswalRank #126 · 17.5K reputationPosted Jun 29, 2014, 8:42 PM EDT
after creating and inserting values in your table, try this
Select Name, Size
from Table t1
INTERSECT
select Name, Size
from Table t2
ON (t1.Name = t2.Name) and (t1.Size = t2.Size)
AS
begin
if ((t1.Name = t2.Name) and (t1.Size = t2.Size))
begin
select Name, Size from Table2
else
select Name, Size from Table1
end
end