DECLARE after IF in TSQL - when does SQL Server allocate resources?
A variable is available for use in a T-SQL batch from the point at which it is declared to the end of the batch. This means that a) variable declarations do not participate in any form of control flow, and b) variable names must be unique within the batch, even if they appear to be in mutually exclusive parts of the batch. SpectralGhost's answer shows the implications of this.
source to share
Distribution is not the right word for him. All local variables only set slots on the stack as part of the function declaration. It took some time to zero out the values, but this is probably done en masse for the entire stack frame.
I wonder when the code is recompiled. This can happen in the middle of a stored procedure for a variety of reasons. The one I was told specifically to look at is changing the temporary table. If you make any changes to the temp table, a new execution plan will be created for the rest of the saved process.
source to share