I just set an SQL Stored Procedure to return a specific row in a specific order (from more to less important):
-- 1st Level -> Query a more detailed object...
SELECT ... WHERE something
-- if a result is find, the SP is returning the correct row.
-- if rowcount = 0 then, the SP is returning an empty row
-- and continues with the next query (less specific than the 1st one).
IF @@ROWCOUNT = 0
BEGIN
SELECT .... WHERE something else...
-- Again, if a result exists, the SP returns the correct row, but also
-- the result of the 1st query is returned without rows...
IF @@ROWCOUNT = 0
BEGIN
SELECT .... WHERE something else again...
END
END
Is there any option to only return the SELECT statement that returns a non empty row? I don’t want to return empty rows… and each time the row is an “sub-level” of the 1st query, I get empty results before the correct row.
I was thinking to create a table variable. Are there any better ways?
Since you are looking for the lack of rows why not use an if/else statement in your procedure. Something like:
Just remember when checking is something exists you should select only one column (for example the id) as if you have calculated columns then this can slow down your procedure.