所以這很奇怪,我在這個SQL位中聲明瞭一個臨時表,但是我只是基於if else
邏輯聲明它。在運行下面的查詢之前,我正在刪除臨時表,並且仍然得到相同的行爲。SQL Server臨時表已存在?
但是,SQL Server與There is already an object named '#ManifestTrackingBranches' in the database.
complaing,當我試圖用我的ReportType
集運行查詢到2
我在這裏失去了一些東西?如果其中ReportType
被設置爲2,並且我試圖選擇到同一個臨時表的第二發生
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
該錯誤。
在開始創建臨時表或使用不同的表名。確定哪些名稱已在批處理中聲明時,T-SQL不注意控制流。 –
@Damien_The_Unbeliever這工作,在邏輯之前創建臨時表。感謝您的解釋!如果你寫這個答案,我會很樂意接受它 – mituw16
你也可以使用表變量。 – podiluska