2014-05-23 2 views
-1

이것은 이상한 일입니다.이 임시 테이블에서 임시 테이블을 선언하고 있지만, 논리를 기반으로 한 번만 선언 할 것입니다. 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 

오류는 두 번째에 발생합니다.

+2

를 추가하거나 다른 테이블 이름을 사용합니다. T-SQL은 배치에서 이미 선언 된 이름을 결정할 때 제어 흐름에주의를 기울이지 않습니다. –

+0

@Damien_The_Unbeliever 이것은 논리 전에 임시 테이블을 만드는 일을했습니다. 설명 주셔서 감사합니다! 이것을 대답에 쓰면 기꺼이 받아 들일 것입니다 – mituw16

+0

또는 테이블 변수를 사용할 수도 있습니다. – podiluska

답변

5

변수 선언 전에이 코드 줄을 추가하십시오. 이 사항이 별도의 배치에있는 경우

IF OBJECT_ID('tempdb..#ManifestTrackingBranches') IS NOT NULL 
DROP TABLE #ManifestTrackingBranches 
GO 

GO 키워드를 사용하여 영향을 미치는 것입니다. 실제로 프로 시저를 작성하고 코드를 다시 실행하여 코드를 테스트 할 때 충분히 좋습니다.

절차 내에서 GO이라는 키워드를 추가 할 수 없으며이 절차가 응용 프로그램에서 호출 될 때 테이블을 삭제할 필요도 없습니다. 이 프로 시저를 호출 할 때마다 자체 연결이 설정되며 해당 연결 범위로 제한된 임시 테이블이 만들어집니다.

+1

이 테스트는 도움이되지 않습니다 .... 저는 @Damien_The_Unbeliever가 옳다고 생각합니다. Tsql은 제어 흐름을 신경 쓰지 않습니다. – Darka

+0

예, 게시 된 코드에 포함시키지 않았지만 스크립트의 맨 위에 넣었습니다. 아무것도 변경하지 마십시오 – mituw16

+0

예. 별도의 배치에 있어야합니다. 테스트하는 동안 키워드 'GO'를 추가해야합니다. 그리고 프로 시저 안에'GO '를 추가 할 수 없습니다. 그러나이 프로 시저가 응용 프로그램에서 호출 될 때 어떤 문제도 발생하지 않습니다. 이 프로 시저를 호출 할 때마다 자체 연결이 이루어지고 해당 연결 범위로 제한된 임시 테이블이 만들어 지므로 drop table 문을 추가 할 필요가 없습니다. –

0

SQL은 연결을 삭제하지 않는 한 연결 당 임시 테이블을 보유합니다. 따라서 일단 임시 테이블을 사용하면 임시 테이블을 삭제하는 것이 좋습니다.

는 시작에 임시 테이블을 만들기 DROP 표 문

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; 

    --clean up after yourself 
    drop table #ManifestTrackingBranches 
    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 

    --clean up after yourself 
    drop table #ManifestTrackingBranches 
    end 
end 
+0

불행히도, 이것은 작동하지 않았습니다. 여전히 동일한 오류가 발생합니다. – mituw16

+0

예. 데이미언 말이 맞아. 그러나 임시 테이블을 명시 적으로 삭제하는 것은 여전히 ​​좋은 생각입니다. 대개는별로 중요하지 않지만 이전에는 불에 타있었습니다. 특히 디버깅 등 – tgolisch

관련 문제