A DECLARE after an IF in TSQL - when does SQL Server allocate resources?
sql-server, t-sql
Solution
It must as this
IF 1=2 BEGIN
DECLARE @BIGTHING AS BIGINT=0
END
select @BIGTHING
runs without problem for me with Microsoft SQL Server 2008 R2. Interestingly though, if the variable is defaulted to a value as shown above, it only sets the value if the IF condition is true.
Problem
If I do the following: ``` IF @THING = TRUE BEGIN DECLARE @BIGTHING AS BIGINT END ``` Will SQL Server allocate the resources for the `@BIGTHING`? Or maybe better asked: does SQL Server parse the stored procedure and allocate all the declared variables before execution?