madcolor
madcolor

Reputation: 8170

SQL Conditional on Bit

Why does this..

DECLARE @SkyBlue Bit
SET @SkyBlue = 1
IF @SkyBlue
    Select 'the sky is blue!'
ELSE
    Select 'the sky is not blue!'

Produce this

"An expression of non-boolean type specified in a context where a condition is expected, near 'Select'."

And is there a Boolean type in SQL2008?

Upvotes: 5

Views: 2190

Answers (1)

Matthew Vines
Matthew Vines

Reputation: 27561

@SkyBlue is a bit, not a boolean. Try:

DECLARE @SkyBlue Bit
SET @SkyBlue = 1
IF @SkyBlue = 1
    Select 'the sky is blue!'
ELSE
    Select 'the sky is not blue!'

Note that this also fails

if 1
    Select 'the sky is blue!'
ELSE
    Select 'the sky is not blue!'

Upvotes: 13

Related Questions