2014-05-21 3 views
0

여기서 교착 상태에 직면하고 있습니다. 문제는 3 개의 다른 임시 테이블을 사용하는 절차를 변경해야한다는 것입니다. 대화 이름을 위해 # temptable1, # temptable2, # temptable3을 허용합니다. 내가 복사/여기에 모든 절차를 붙여하지만 일반적인 생각이있다 할 수없는, 원래 절차 (procedure1는)IF ELSE 문 내에서 임시 테이블 삭제

create table #temptable1 

는 다음 선택을 사용하여 나머지 두를 생성하는 과정의 시작 부분에있는 # temptable1를 생성 /이 잘 작동 점,하지만 내가 원하는까지 문

select T.Col 
    , T.Col2 
    , T.Col3 
into #temptable2 
from table1 T 
where T.BB>0 

select T.Col 
    , T.Col2 
    , T.Col3 
into #temptable3 
from table2 T 
where T.BB>0 

drop table #temptable1 
drop table #temptable2 
drop table #temptable3 

로는 IF/else 문을 추가하여 절차를 변경하는 것입니다. 나는이 메시지가 새로운 프로 시저를 만들려고 할 때 임시 테이블 if 문 (@ 경우 초 이내에 어디에 따라서, 그런 선이 표시됩니다

Msg 2714, Level 16, State 1, Procedure pPortfoliostest3, Line 412 
There is already an object named '#temptable1' in the database. 
Msg 2714, Level 16, State 1, Procedure pPortfoliostest3, Line 550 
There is already an object named '#temptable2' in the database. 
Msg 2714, Level 16, State 1, Procedure pPortfoliostest3, Line 711 
There is already an object named '#temptable3' in the database. 

declare @BBB nvarchar(32) 

create table #temptable1 


if @BBB='dd' 

begin 

select T.Col 
     , T.Col2 
     , T.Col3 
    into #temptable2 
    from table1 T 
    where T.BB>0 and T.G='FDL' 

    select T.Col 
     , T.Col2 
     , T.Col3 
    into #temptable3 
    from table2 T 
    where T.BB>0 and T.G='FDL' 

    drop table #temptable1 
    drop table #temptable2 
    drop table #temptable3 
end 

if @BBB='kk' 

begin 

select T.Col 
     , T.Col2 
     , T.Col3 
    into #temptable2 
    from table1 T 
    where T.BB>0 and T.G='FD' 

    select T.Col 
     , T.Col2 
     , T.Col3 
    into #temptable3 
    from table2 T 
    where T.BB>0 and T.G='FD' 

    drop table #temptable1 
    drop table #temptable2 
    drop table #temptable3 
end 

else 

begin 

select T.Col 
     , T.Col2 
     , T.Col3 
    into #temptable2 
    from table1 T 
    where T.BB>0 

    select T.Col 
     , T.Col2 
     , T.Col3 
    into #temptable3 
    from table2 T 
    where T.BB>0 

    drop table #temptable1 
    drop table #temptable2 
    drop table #temptable3 
end 

를 볼 수 있습니다 BBB = 'kk'). 나는 다른 조합을 시도했지만 아무 소용이 없습니다. 어떤 팁? 시간 내 주셔서 감사합니다.

+1

코드 시작 부분에'drop table 문을 넣으십시오. ' –

+0

시도해 보겠습니다. – Jespar

+0

nop하지만 어쨌든 고마워요 – Jespar

답변

3

T-SQL 구문 분석기는 매우 원시적입니다. 특히 컨트롤 흐름은 객체 이름이 범위에 포함되어 범위에 남아있는 경우 영향을 미치지 않습니다.

따라서 if 브랜치에서 사용중인 이름은 여전히 ​​범위에 있으므로 else 브랜치가 구문 분석 될 때 충돌이 발생합니다.

제어 흐름 전에 테이블 정의를 저장 프로 시저의 맨 위로 이동하고 SELECT ... INTO ...이 아닌 INSERT ... SELECT ...으로 전환하십시오. 테이블 정의가 ifelse 분기 사이에서 일치하지 않으면 임시 테이블에 다른 이름을 사용해야합니다. 파서가 얼마나 원시의 또 다른 예로서


는 다음을 고려 : (당신이 예상 했겠지만)

if 1=0 
begin 
    declare @a int 
end 
select @a 

이것은보다는 없다는 오류를 null을 포함한 결과 세트를 생산을 @a ISN 선언하지 않았다.

+0

대단히 고마워요. – Jespar