I’d like to check if there is anything to return given a number to check against, and if that query returns no entries, increase the number until an entry is reached and display that entry. Currently, the code looks like this :
SELECT *
FROM news
WHERE DATEDIFF(day, date, getdate() ) <= #url.d#
ORDER BY date desc
where #url.d# is an integer being passed through (say 31). If that returns no results, I’d like to increase the number stored in #url.d# by 1 until an entry is found.
This kind of incremental querying is just not efficient. You’ll get better results by saying – “I’ll never need more than 100 results so give me these” :
Then filtering further on the client side if you want only a particular day’s items (such as the items with a common date as the first item in the result).
Or, you could transform your multiple query request into a two query request: