这很奇怪,我在这段SQL中声明了一个临时表,但基于if else逻辑,我只声明了一次。在运行下面的查询之前,我会删除临时表,但仍然出现相同的问题。
然而,当我将ReportType设置为2并尝试运行查询时,SQL Server会报告“数据库中已经存在名为'#ManifestTrackingBranches'的对象。”
我是否遗漏了什么?
T-SQL
declare @ReportType int
declare @CustomerNumber int
declare @StartDate datetime
declare @EndDate datetime
set @ReportType = 2
set @CustomerNumber = 81
set @StartDate = '2014-04-27'
set @EndDate = '2014-05-04'
if @CustomerNumber = 81
begin
if @ReportType = 1 -- roll up by location
begin
select InboundData.Tracking,
InboundData.NegotiatedRate
into #ManifestTrackingBranches
from InboundData
where Injected >= @StartDate and Injected <= @EndDate
-- Match tracking numbers against Ebill Data
select #ManifestTrackingBranches.Tracking,
SUM(isnull(cast(#ManifestTrackingBranches.NegotiatedRate as decimal(18,2)),0)) as ManifestAmount
from EBillData
group by #ManifestTrackingBranches.Branch
end
else if @ReportType = 2 -- Line Item Reports
begin
select InboundData.Tracking,
InboundData.NegotiatedRate
into #ManifestTrackingBranches
from InboundData
where Injected >= @StartDate and Injected <= @EndDate
-- Match tracking numbers against Ebill Data
select #ManifestTrackingBranches.Tracking,
SUM(isnull(cast(#ManifestTrackingBranches.NegotiatedRate as decimal(18,2)),0)) as ManifestAmount
from EBillData
end
end
出错发生在第二个if语句中,当ReportType
被设置为2时,我试图选择同一个临时表。