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.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Abhishek JaiswalPosted Jun 30, 2014, 12:42 AM
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
Khan Abrar AhmedPosted Jun 18, 2014, 9:56 AM
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