在存储过程中,何时在SQL Server 2005中创建#Temptable?在创建查询执行计划或执行存储过程时?SQL Server 2005临时表
if (@x = 1)
begin
select 1 as Text into #Temptable
end
else
begin
select 2 as Text into #Temptable
end
在存储过程中,何时在SQL Server 2005中创建#Temptable?在创建查询执行计划或执行存储过程时?SQL Server 2005临时表
if (@x = 1)
begin
select 1 as Text into #Temptable
end
else
begin
select 2 as Text into #Temptable
end
有趣的问题。
对于您正在创建的临时表的类型,我认为这是存储过程执行的时间。使用#前缀创建的表可供创建的SQL Server会话访问。一旦会话结束,它们将被删除。
此网址:http://www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx似乎表明在创建查询执行计划时不会创建临时表。
它在执行时创建并在会话结束时被删除。
虽然它可能在会话结束时自动丢弃,但最好在练习完成后自行放下表格。
您可能还想考虑表变量,其生命周期完全由您来管理。
DECLARE @MyTable TABLE (MyPK INT IDENTITY, MyName VARCHAR(100))
INSERT INTO @MyTable (MyName) VALUES ('Icarus')
INSERT INTO @MyTable (MyName) VALUES ('Daedalus')
SELECT * FROM @MyTable
我几乎总是使用这种方法,但它确实有缺点。最值得注意的是,你只能使用你可以在TABLE()结构中声明的索引,这意味着你仅限于主键 - 不使用ALTER TABLE。