이것은 이상한 일입니다.이 임시 테이블에서 임시 테이블을 선언하고 있지만, 논리를 기반으로 한 번만 선언 할 것입니다. if else
. 아래 쿼리를 실행하기 전에 임시 테이블을 삭제할 때 여전히 동일한 동작이 발생합니다. 내가 여기서 뭔가를 놓치고 2SQL Server 임시 테이블이 이미 있습니까?
내가 내 ReportType
세트로 쿼리를 실행하려고하면
그러나, SQL 서버, There is already an object named '#ManifestTrackingBranches' in the database.
와 complaing입니까? 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