views:

52

answers:

2

Here is the T-SQL code that I coded, which I imagined to work but didn't work:

DECLARE @Local nvarchar(20)
SET @Local = 'Yes'

SELECT 
(CASE WHEN @Local = 'Yes' THEN ID ELSE COUNT(ID) END)
FROM myTable

Is there something I am doing wrong or is there any other way to do that?

Thanks.

+2  A: 
IF ( @Local = 'Yes' )
    SELECT ID FROM MyTable
ELSE
    SELECT COUNT(ID) FROM MyTable
AdaTheDev
A: 

The 2 queries are fundamentally different so "no", in the way you posted

You can do both in one call

SELECT 
ID
FROM myTable
COMPUTE SUM(ID)

Or use @@ROWCOUNT

Or use AdaTheDev's solution

gbn