if (@DiscountFrom = '100' and @DiscountUpto = '100')
begin
select id1.*, case when id1.Module_Type='sl' and id2.Module_Type = 'sl' and id1.Module_Code = id2.Module_Code and id1.Product_Code = id2.Product_Code and id2.Discount_Percentage = 0 and id2.Is_Inactive = 0 then id2.Quantity_Out else 0 end as BaseQuantity
into #invoices2
from batches id1
right join batches id2 on id1.Module_Code = id2.Module_Code and id1.Product_Code = id2.Product_Code and id1.Discount_Percentage= '100'
select * into #batches2 from #invoices2
where BaseQuantity <> 0
drop table #invoices2
else
select * into #batches2 from Batches