为什么 SQL Server 中的临时表没有从 tempdb 中删除?
我创建了一个包含 7 个临时表的存储过程,每个临时表在其工作结束时都会被删除。
我从一个 Web 服务调用 SP,并且我们在不同的实例中使用相同的 Web 服务。
我已强制删除每个临时表,但是当 SP 执行时,它不会删除位于“tempdb/临时表”中的任何临时表。而且,当我打开应用程序的新实例并尝试执行相同的 SP 时,它将修改相同的临时表。
这给我带来了问题。当 SP 同时执行时,它将锁定表,它将锁定表,并且我的 sp 无法产生结果并抛出异常。
所以我想在操作结束时删除临时表。请帮忙。
I have created one stored procedure with 7 temporary tables and each temp table is dropped at the end of their own work.
I am calling the SP from one web service and same web service we are used for different instance.
I have dropped every temp table forcefully but when SP executes it will not delete any of the temporary table which are located in "tempdb/Temporary Table". And, when I open new instance of my application and try to execute same SP it will modify same temp tables.
This creates problem for me. it will lock the tables when SP execute simultaneously it will lock the table and my sp is not able to produce result and throw exception.
So I want to drop my temporary tables at the end of my operation. please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我无法告诉你为什么会发生这种情况,但我以前也处理过这个问题。
尝试在 SP 的开头或结尾清理表或使用表变量。
I can't tell you why this is happening, but I have dealt with it before as well.
Try cleaning up your tables at the beginning or end of the SP or using table variables.
如果您使用了许多临时表并且您的 sp 和 drop 语句无法执行之间存在一些错误,则可能会发生这种情况。
使用始终是最佳实践。
因此,在 SP 启动时
This can occur in case if you have used many Temp tables and you have some Error in between of Your sp and your drop statement could not executed.
So its always best practice to use
in start of SP.
要强制删除临时表,请使用
丑陋但有效的方法。为每个临时表使用单独的 TRY。
To force dropping of temp tables use
Ugly but effective. Use a separate TRY for each temporary table.
我认为这不会起作用,因为我们知道 sql server 存储临时表名称并添加一些额外的字符。
如果存在(从 tempdb.sys.tables 中选择 1,其中名称如“#TableName%”)
删除表#TableName
I think this will not work as we know sql server store temp table name with adding some extra character.
if exists(select 1 from tempdb.sys.tables where name like '#TableName%')
DROP TABLE #TableName