jeudi 29 janvier 2015

MS SQL IF statement on query results

I have the results of the query below emailed everyday 90% of the time empty. I would like to add an if statement to only send the email If (resulting row count>0)



Select Orders.TransactionNumber, Orders.RepNumber, Orders.CustomerID,
Orders.ShipToId, orders.ItemCode, Orders.Quantity, Orders.ReceivedDate,
Orders.TransmitStatus from (select TransactionNumber from Orders
group by TransactionNumber
having COUNT (TransactionNumber)=1) as transa

Inner join Orders on Orders.TransactionNumber=transa.TransactionNumber

where ItemCode=9987 and ReceivedDate > DateADD (day, -4, GetDate() )
Order by ReceivedDate


Add here> If (counted rows>0 send the email else end)


Aucun commentaire:

Enregistrer un commentaire