gpt4 book ai didi

sql - 为什么不能在一次事务中创建和删除表两次?

转载 作者:行者123 更新时间:2023-12-04 02:16:31 24 4
gpt4 key购买 nike

这会抛出错误

There is already an Object named '##TempComment' in the Database.

DROP TABLE ##TempComment
CREATE TABLE ##TempComment
(
TagValue NvarChar(MAX)
);
DROP TABLE ##TempComment
CREATE TABLE ##TempComment
(
TagValue NvarChar(MAX)
);

上面是问题的简单表述方式,但是下面你可以看到我为什么这样做。

我知道这是一个奇怪的请求,但这里有更多关于我正在努力实现的目标。

  DECLARE @ValuationId INT = 20897
DECLARE @Count INT = 0
DECLARE @isCompSale NVARCHAR(MAX)
DECLARE @Comment nvarchar(250)

DROP TABLE ##TempComment
SELECT TagValue
INTO ##TempComment
FROM [FormValueLive_sql].[dbo].[ValuationDetail]
WHERE TagName IN ('sale_1_erf','sale_1_portion','sale_1_township', 'comparable_sale_1_sales_price', 'comparable_sale_1_sales_date', 'sale_1_overall') AND ValuationId = @ValuationId
SET @isCompSale = (SELECT TagValue FROM [FormValueLive_sql].[dbo].[ValuationDetail] WHERE TagName = 'sale_1_use_as_comparable' AND ValuationId = @ValuationId)

IF @isCompSale = 'Yes' AND @Count < 3
Begin
SELECT @Comment = COALESCE(@Comment + ',','') + TagValue FROM ##TempComment
SET @Count = @Count + 1
END

SET @isCompSale = 'No'

--So Comments is my collective and only if a condition is met do i take the values of the temp table.

DROP TABLE ##TempComment
SELECT TagValue
INTO ##TempComment
FROM [FormValueLive_sql].[dbo].[ValuationDetail]
WHERE TagName IN ('sale_6_erf','sale_6_portion','sale_6_township', 'comparable_sale_6_sales_price', 'comparable_sale_6_sales_date', 'sale_6_overall') AND ValuationId = @ValuationId

SET @isCompSale = (SELECT TagValue FROM [FormValueLive_sql].[dbo].[ValuationDetail] WHERE TagName = 'sale_6_use_as_comparable' AND ValuationId = @ValuationId)

IF @isCompSale = 'Yes' AND @Count < 3
Begin
SELECT @Comment = COALESCE(@Comment + ', ','') + TagValue FROM ##TempComment
SET @Count = @Count + 1
END

SELECT @Comment

所以 Comments 是我的集体,只有满足条件时,我才会采用临时表的值。我在估值中有 20 次销售,其中一个字段检查是否已选择销售,只有当我知道这是真的时,我才会获取临时值数据。

最佳答案

TL;DR 解析器给出错误并且未在批处理中运行任何命令。

为什么不能在一次事务中创建和删除表两次?可以。问题是在同一批处理中进行两次创建。

一个事务,两个批处理:(有效)

BEGIN TRANSACTION

CREATE TABLE ##TempComment
(
TagValue NvarChar(MAX)
);
GO
DROP TABLE ##TempComment
CREATE TABLE ##TempComment
(
TagValue NvarChar(MAX)
);
COMMIT TRANSACTION

两笔交易,一批:(无作品。)

BEGIN TRANSACTION
CREATE TABLE ##TempComment
(
TagValue NvarChar(MAX)
);
COMMIT TRANSACTION
DROP TABLE ##TempComment
CREATE TABLE ##TempComment
(
TagValue NvarChar(MAX)
);
COMMIT TRANSACTION
GO

下一个证据,新的表名只是为了干净:

CREATE TABLE #t (c INT)
DROP TABLE #t
CREATE TABLE #t (c INT)

我们返回错误,消息 3701,级别 11,状态 5,第 1 行无法删除表“#t”,因为它不存在或您没有权限。

现在,放下#t,DROP TABLE #t .我们得到,无法删除表“#t”,因为它不存在或您没有权限。我们被告知无法创建表 #t,因为它已经存在,即使它从未被创建。

SQL Server 解析器看到了两个创建语句,没有考虑删除并决定在执行任何实际工作之前会发生错误。 SQL Server 仅对临时表执行此操作,永久表的创建和删除工作。

我不明白您的用例,并且认为全局临时表可能是错误的选择。但是,您可以通过将创建的内容放入字符串中并动态运行它们来获得所需的效果,这会将解析放入单独的批处理中。

EXEC ('CREATE TABLE ##TempComment
(
TagValue NvarChar(MAX)
)');
DROP TABLE ##TempComment
EXEC ('CREATE TABLE ##TempComment
(
TagValue NvarChar(MAX)
)');

关于sql - 为什么不能在一次事务中创建和删除表两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33417389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com