I have a query that looks like this:
use [dbName]
DECLARE @profile_id int
SET @profile_id = 18
DECLARE @test int
SET @test = (SELECT user_profile_id FROM tbl_profiles WHERE id = @profile_id)
SELECT @test as test
if @test = NULL
SELECT 1
else
SELECT 2
I know that the value in the table is null, and I can see that @test is correctly being set to that value because the first SELECT shows:
|test
1|NULL
However, in the IF condition the variable is not being treated as NULL because only the third SELECT is returned, showing:
|(no column name)
1| 2
Why is that?
You need to use
IS NULL(not= NULL)You can never compare
NULLwith the “regular” comparison operators – you always have to useIS NULLorIS NOT NULLonly.And
NULLreally isn’t a value per se – it’s the absence of a value…