临时表的 DROP TABLE 失败
我有一个客户端应用程序,它创建一个临时表,对临时表执行批量插入,然后在删除该表之前使用该表执行一些 SQL。
伪代码:
open connection
begin transaction
CREATE TABLE #Temp ([Id] int NOT NULL)
bulk insert 500 rows into #Temp
UPDATE [OtherTable] SET [Status]=0 WHERE [Id] IN (SELECT [Id] FROM #Temp) AND [Group]=1
DELETE FROM #Temp WHERE [Id] IN (SELECT [Id] FROM [OtherTable] WHERE [Group]=1)
INSERT INTO [OtherTable] ([Group], [Id]) SELECT 1 as [Group], [DocIden] FROM #Temp
DROP TABLE #Temp
COMMIT TRANSACTION
CLOSE CONNECTION
此操作失败,DROP 语句出现错误:
无法删除表“#Temp”,因为它不存在或您没有权限。
我无法想象在没有其他事情发生的情况下如何发生这种故障,但我没有看到在此之前发生任何其他故障。
我是否遗漏了什么可能导致这种情况发生?
I have a client application that creates a temp table, the performs a bulk insert into the temp table, then executes some SQL using the table before deleting it.
Pseudo-code:
open connection
begin transaction
CREATE TABLE #Temp ([Id] int NOT NULL)
bulk insert 500 rows into #Temp
UPDATE [OtherTable] SET [Status]=0 WHERE [Id] IN (SELECT [Id] FROM #Temp) AND [Group]=1
DELETE FROM #Temp WHERE [Id] IN (SELECT [Id] FROM [OtherTable] WHERE [Group]=1)
INSERT INTO [OtherTable] ([Group], [Id]) SELECT 1 as [Group], [DocIden] FROM #Temp
DROP TABLE #Temp
COMMIT TRANSACTION
CLOSE CONNECTION
This is failing with an error on the DROP statement:
Cannot drop the table '#Temp', because it does not exist or you do not have permission.
I can't imagine how this failure could occur without something else going on first, but I don't see any other failures occurring before this.
Is there anything that I'm missing that could be causing this to happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
中间的会话中可能发生了一些事情吗?
尝试在删除表之前检查该表是否存在:
possibly something is happening in the session in between?
Try checking for the existence of the table before it's dropped:
我已经在 SQL Server 2005 上对此进行了测试,您可以在创建它的事务中删除临时表:
您使用的是哪个版本的 SQL Server?
您可能会重新考虑为什么要删除临时表。当连接结束时,本地临时表会自动删除。通常不需要明确删除它。
全局临时表以双散列开头(fe
##MyTable
。),但即使是全局临时表,当没有连接引用它时也会自动删除。I've tested this on SQL Server 2005, and you can drop a temporary table in the transaction that created it:
Which version of SQL Server are you using?
You might reconsider why you are dropping the temp table at all. A local temporary table is automatically deleted when the connection ends. There's usually no need to drop it explicitly.
A global temporary table starts with a double hash (f.e.
##MyTable
.) But even a global temp table is automatically deleted when no connection refers to it.我认为您根本没有创建表,因为该语句
不正确。请把它写成
并看看它是否有效。
I think you aren't creating the table at all, because the statement
is incorrect. Please, write it as
and see if it works.
注意:请在
TABLE_NAME
处输入您的表名称,在DATABASE_NAME
处输入数据库名称Note:Please enter your table name where
TABLE_NAME
and database name where it saysDATABASE_NAME
多年来没有一个明确的答案:您一定在创建或删除表的两行之一中写错了表的名称。
当然,您在发布之前已经更改了表的名称。这就是为什么这里没有人可以帮助你。
#Temp
可能类似于#mylongtmptablename
。您可能会拥有#mylongtablename
。我说得这么清楚,因为它发生在我身上,我一次又一次地尝试,甚至在第二次看到这个名字时都认为这个名字是正确的,但事实并非如此。
After many years without a clear answer to this: you must have written the name of the table wrong in one of the two lines that create or drop the table.
For sure, you have changed the name of the table before you published it. And that is why nobody could help you here.
#Temp
is likely something like#mylongtmptablename
. And you will likely have#mylongtablename
.I am saying that so clearly since it happened to me, and I tried it again and again, thinking even at second sight that the name was right, but it was not.