Detect variable declared in potentially unreachable scope
Unlike a proper programming language TSQL allows you to declare a variable inside an IF block (or some other scope) and then use the variable outside that IF block, even if the IF block is not entered during run time. This can lead to errors if the code outside the IF is testing for certain values.
It would be good if Code Analysis could detect this as a potentially dangerous coding structure.
E.g.
IF 1 = 0
BEGIN
DECLARE @Var VARCHAR(50) = '123';
END;
SELECT @Var
1
vote
James Penman
shared this idea