考虑以下伪 T-SQL 代码(由存储过程执行):
CREATE TABLE #localTable ...
<do something with the temporary table here>
DROP TABLE #localTable;
DROP TABLE
语句是存储过程执行的最后一条语句——该语句有什么好处吗?
请注意,我并不是在询问在存储过程的中间删除临时表(本地或非本地)(即在不再需要表之后,但在存储过程结束之前)过程代码)——由于减少了继续执行存储过程所需的内存,这似乎具有重要的好处。我想知道显式删除表与在存储过程完成执行时“让”SQL Server 执行此操作相比是否有任何好处(或任何影响,实际上是积极的还是消极的)。
最佳答案
有一篇关于此的详细文章 here .
The temporary object is renamed to an internal form when DROP TABLE is executed, and renamed back to the same user-visible name when CREATE TABLE is encountered on the next execution. In addition, any statistics that were auto-created on the temporary table are also cached. This means that statistics from a previous execution remain when the procedure is next called.
关于sql - 在存储过程结束时显式删除本地临时表有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4670924/