临时表的DROP TABLE失败
我有一个客户端应用程序,该应用程序创建一个临时表,然后向该临时表中进行批量插入,然后在删除该表之前使用该表执行一些SQL.
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.
伪代码:
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语句上出现错误:
This is failing with an error on the DROP statement:
无法删除表"#Temp",因为该表不存在或您没有权限.
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?
之间的会话中是否可能发生了某些事情?
possibly something is happening in the session in between?
尝试在删除表之前检查该表是否存在:
Try checking for the existence of the table before it's dropped:
IF object_id('tempdb..#Temp') is not null
BEGIN
DROP TABLE #Temp
END