临时表的 DROP TABLE 失败

发布于 2024-08-29 03:03:17 字数 655 浏览 2 评论 0原文

我有一个客户端应用程序,它创建一个临时表,对临时表执行批量插入,然后在删除该表之前使用该表执行一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仲春光 2024-09-05 03:03:17

中间的会话中可能发生了一些事情吗?

尝试在删除表之前检查该表是否存在:

IF object_id('tempdb..#Temp') is not null
BEGIN
   DROP TABLE #Temp
END

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
雨落□心尘 2024-09-05 03:03:17

我已经在 SQL Server 2005 上对此进行了测试,您可以在创建它的事务中删除临时表:

begin transaction
create table #temp (id int)
drop table #temp
commit transaction

您使用的是哪个版本的 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:

begin transaction
create table #temp (id int)
drop table #temp
commit transaction

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.

梦太阳 2024-09-05 03:03:17

我认为您根本没有创建表,因为该语句

CREATE TABLE #Temp ([Id] AS int)

不正确。请把它写成

CREATE TABLE #Temp ([Id] int)

并看看它是否有效。

I think you aren't creating the table at all, because the statement

CREATE TABLE #Temp ([Id] AS int)

is incorrect. Please, write it as

CREATE TABLE #Temp ([Id] int)

and see if it works.

心欲静而疯不止 2024-09-05 03:03:17
BEGIN TRAN

IF object_id('DATABASE_NAME..#TABLE_NAME') is not null
BEGIN
   DROP TABLE #TABLE_NAME
END

COMMIT TRAN

注意:请在 TABLE_NAME 处输入您的表名称,在 DATABASE_NAME 处输入数据库名称

BEGIN TRAN

IF object_id('DATABASE_NAME..#TABLE_NAME') is not null
BEGIN
   DROP TABLE #TABLE_NAME
END

COMMIT TRAN

Note:Please enter your table name where TABLE_NAME and database name where it says DATABASE_NAME

陈甜 2024-09-05 03:03:17

多年来没有一个明确的答案:您一定在创建或删除表的两行之一中写错了表的名称。

当然,您在发布之前已经更改了表的名称。这就是为什么这里没有人可以帮助你。

#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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文