Deepak M Deepak M - 30 days ago 16
SQL Question

MS SQL: How union works inside if exists condition?

Can anyone explain how union works inside if exists? My code is below.

if exists(select top 1 ShoppingCartNo from tbSupplierLineItem
where BidSummaryDueDate>CONVERT(date,getdate()) and ShoppingCartNo=@SC
union
select top 1 ShoppingCartNo from Protrac_Archieve.dbo.tbSupplierLineItem
where BidSummaryDueDate>CONVERT(date,getdate()) and ShoppingCartNo=@SC)
begin
SQL statements
end

Answer Source

It first combines the result sets of two select queries inside if exists statement and then checks for the existence of any record from those queries.