This will throw the error
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)
);
The above is a simple way of putting the problem, but below you can see why i am doing this way.
I am aware this is an odd request but here is more on what i am trying to achieve.
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
So Comments is my collective and only if a condition is met do i take the values of the temp table. I have 20 sales in a valuation and one of the fields checks if the sale has been selected only if i know that to be true do i take the temp value data.