I want to write a stored procedure that works something like this:
SELECT * from T where T.A = @a and T.B = @b
if that returns rows, return those rows, if not, return
SELECT * from T where T.A = @a and T.B IS NULL
Edit:
It feels that there should be a way to create a procedure such that it runs the first query once and runs the second query only if necessary.
End Edit.
The best I could manage was the follow, which (in theory) runs the first query twice, unless maybe its cached:
IF EXISTS (SELECT * from T where T.A = @a and T.B = @b) THEN
SELECT * from T where T.A = @a and T.B = @b
ELSE
SELECT * from T where T.A = @a and T.B IS NULL
For what its worth, this is in Microsoft SQL Server 2008