I have a stored procedure that I want to perform a different select
based on the result stored in a local variable. My use case is simply that, on certain results from a previous query in the stored procedure, I know the last query will return nothing. But the last query is expensive, and takes a while, so I'd like to short circuit that and return nothing.
Here is a mock-up of the flow I want to achieve, but I get a syntax error from SQL Management Studio
DECLARE @myVar int;
SET @myVar = 1;
CASE WHEN @myVar = 0
THEN
SELECT 0 0
ELSE
SELECT getDate()
END
The error is: Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'CASE'.
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near 'END'.