John Zumbrum
John Zumbrum

Reputation: 2856

SELECT IF into custom variable

I have this:

SELECT 
        @is_daily_rollup = CASE WHEN rt.[id]=1 THEN 1 ELSE 0,
        @is_weekly_rollup = CASE WHEN rt.[id]=2 THEN 1 ELSE 0

But sql server is complaining about syntax. How would I go about implementing this conditional value into a variable?

Upvotes: 0

Views: 87

Answers (2)

Lamak
Lamak

Reputation: 70658

You are missing the END for your CASE:

SELECT 
        @is_daily_rollup = CASE WHEN rt.[id]=1 THEN 1 ELSE 0 END,
        @is_weekly_rollup = CASE WHEN rt.[id]=2 THEN 1 ELSE 0 END

Of course, this is assuming that you already declared yor variables.

Upvotes: 4

Taryn
Taryn

Reputation: 247810

For a CASE statement you need to provide an END

CASE WHEN rt.[id]=1 THEN 1 ELSE 0 END
CASE WHEN rt.[id]=2 THEN 1 ELSE 0 END

so your full query would be:

SELECT @is_daily_rollup = CASE WHEN rt.[id]=1 THEN 1 ELSE 0 END,
       @is_weekly_rollup = CASE WHEN rt.[id]=2 THEN 1 ELSE 0 END

Upvotes: 4

Related Questions