Stored Procedure T-SQL If boolean check
The corresponding sql data type to boolean is bit, meaning 1 for true and 0 for false, so:
IF( @Statement=1)
BEGIN
SELECT COUNT(*) FROM Table
END
ELSE
BEGIN
SELECT MIN(ID) FROM Table
END
END
DECLARE @bool BIT = 1
IF @bool = 1
BEGIN
-- do stuff here
PRINT 'it was true';
END
ELSE
BEGIN
-- do other stuff here
PRINT 'it was not true';
END
If you've only got a single line inside the if then you don't need the BEGIN
and END
, but it's probably good practice to use them anyway.
Starting with SQL Server 2012 IIF is shorthand way of writing IF THEN ELSE for assignment:
DECLARE @str VARCHAR(10);
SET @str = IIF(1=1, 'Yes', 'No');