I can't seem to figure out how to load the value returned by the following expression/subquery into a variable:
declare @var int
set @var = null
IF @var IS NULL
SELECT @var = t.col_one
FROM my_table t
WHERE t_datetime = (SELECT MAX(t_datetime) FROM t WHERE t.col_two = 1)
How can I load the result of the expression into the variable?
I've updated the code to reflect answers below however the issue has persisted. There are no errors but later in my sproc when I call @var the variable is still null. Which means this still isn't working. Later in the code I'm using:
t.col_three = @var
Further I'm not using t.col_three = @var or @var is null because the variable cannot be null:
t.col_three = @var or @var is nullnothing is returned. When I do use that, NULL is returned.NULLif that's the case...