The answer is simple:
There is no real boolean data type in SQL Server. There is BIT
, but this is just an integer value reduced to 0
and 1
and you always have to compare this with your target value.
This would not work:
DECLARE @bit BIT=1;
IF @bit DoSomething;
You'd have to use
IF @bit=1 DoSomething;
Same with CASE WHEN @bit=1 THEN ...
But there is a boolean actually, like we know this from EXISTS
:
IF EXISTS(SELECT 1 FROM SomeWhere) DoSomething;
But this is not a data type one could use directly... Even a boolean scalar function you'd declare with RETURNS BIT
and you'd have to use the return value with a comparison against 0 or 1.
Specialty 1
This will not return false but rather fill xyz into @tst
DECLARE @tst VARCHAR(100)='test';
SELECT @tst='xyz';
SELECT @tst;
With paranthesis SQL Server tries to take the value, but the comparison has no value...
SELECT (@tst='xyz'); --error
Specialty 2
This will use the left side as alias
SELECT test=Column FROM Table;
Solution with CASE
This will update just the second row
DECLARE @tbl TABLE(c1 int,c2 int, c3 int);
INSERT INTO @tbl VALUES(1,1,1),(2,2,3),(4,4,4);
UPDATE @tbl SET c1=CASE WHEN c2<>c3 THEN 99 ELSE c1 END
SELECT * FROM @tbl
More Examples:
These are forbidden
SET @bit=EXISTS(SELECT 1 FROM @tbl WHERE c1=50);
SET @bit=(EXISTS(SELECT 1 FROM @tbl WHERE c1=50));
SET @bit=(SELECT EXISTS(SELECT 1 FROM @tbl WHERE c1=50));
But this is allowed
SET @bit=CASE WHEN EXISTS(SELECT 1 FROM @tbl WHERE c1=50) THEN 1 ELSE 0 END;
There is a related question, you might to read further: This related quesiton covers this aspect too: Is there a Boolean data type in Microsoft SQL Server like there is in MySQL?