In a stored procedure, when is #Temptable created in SQL Server 2005? When creating the query execution plan or when executing the stored procedure?
if (@x = 1) begin select 1 as Text into #Temptable end else begin select 2 as Text into #Temptable end
Interesting question.
For the type of temporary table you’re creating, I think it’s when the stored procedure is executed. Tables created with the # prefix are accessible to the SQL Server session they’re created in. Once the session ends, they’re dropped.
This url: http://www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx seems to indicate that temp tables aren’t created when query execution plans are created.