2013-02-20 4 views
1

에서 다음 번 쿼리 이상을 실행할 수 없습니다, 나는이 내가 그것을 처음 실행할 때 작동하지만, 내가 다시 실행하려고하면, 나는 오류를 얻을이 쿼리SQL Server 2008을 사용하여 SQL

if (exists (select * from tempdb.INFORMATION_SCHEMA.TABLES where TABLE_NAME = '##a12')) 
begin 
drop table ##a12 
end 
else 
declare @p varchar(1000) 
declare @s varchar(5) 
declare @xx2 varchar(2000) 

set @p = '64-267-601' 
select top 1 @p=ivproduct from rpiv where [email protected] or [email protected] 
select distinct storeid,color,wil,cast('OH' as varchar(4)) as TP into ##a12 from rpiv i, rpproducts p where i.ivproduct=p.productid and [email protected] 
insert into ##a12 select storeid,color,wil,'Rcvd' from ##a12 
insert into ##a12 select storeid,color,wil,'Sld' from ##a12 where tp='oh' 

DECLARE myCursor CURSOR FOR SELECT distinct size from rpiv where [email protected] 
open MyCursor 
FETCH NEXT FROM myCursor into @S 
WHILE @@FETCH_STATUS = 0 
    begin 
    set @xx2='alter table ##a12 add ['[email protected]+'] varchar(5)' 
    exec(@xx2) 
    set @xx2='update a set a.['[email protected]+']=coalesce(b.onhand,0) from ##a12 a,rpiv b where a.tp=''oh'' and a.storeid=b.storeid and a.color=b.color and a.wil=b.wil and b.size='''[email protected]+'''' 
    exec(@xx2) 
    set @xx2='update a set a.['[email protected]+']=coalesce(b.PurchasedToDate,0) from ##a12 a,rpiv b where a.tp=''rcvd'' and a.storeid=b.storeid and a.color=b.color and a.wil=b.wil and b.size='''[email protected]+'''' 
    exec(@xx2) 
    set @xx2='update a set a.['[email protected]+']=coalesce(b.SoldtoDate,0) from ##a12 a,rpiv b where a.tp=''sld'' and a.storeid=b.storeid and a.color=b.color and a.wil=b.wil and b.size='''[email protected]+'''' 
    exec(@xx2) 
    set @xx2='update ##a12 set ['[email protected]+']='''' where ['[email protected]+'] =''0''' 
    exec(@xx2) 
    set @xx2='update ##a12 set ['[email protected]+']='''' where ['[email protected]+'] is null' 
    exec(@xx2) 

    FETCH NEXT FROM myCursor into @S 
    End 
Close myCursor 
DEALLOCATE myCursor 
if not exists(select * from ##a12 where wil<>'') 
begin 
alter table ##a12 drop column wil 
select * from ##a12 order by storeid,color,tp 
end 
else 
select * from ##a12 order by storeid,color,wil,tp 
내가 관리 스튜디오를 다시로드하는 경우, 그것은 잘 작동, 그리고 수동으로 테이블을 삭제할 경우뿐만 아니라 작동

Msg 207, Level 16, State 1, Line 14 
Invalid column name 'wil'. 
Msg 213, Level 16, State 1, Line 14 
Column name or number of supplied values does not match table definition. 

:

는 그리고 이것은 내가 오류입니다. 전에는 문제없이 시작하기 전에 drop 문을 사용했지만 어떤 이유로 그것을 지금 사용하지 않습니다.

+1

## 글로벌 임시 테이블을 사용하는 이유는 무엇입니까? 왜 # 로컬이 아닌가? 왜 끝나면 명시 적으로 테이블을 떨어 뜨리지 않는거야? –

+0

동적 SQL은 외부와 통신하기 위해 ##가 필요합니다. – JohnZ

+0

@JohnZ 동적 SQL 내에서 'EXEC()'보다 더 높은 범위에서 생성하기 때문에 여전히 로컬 임시 테이블을 참조 할 수 있어야합니다. –

답변

2

보십시오 추가, GO :

Close myCursor 
DEALLOCATE myCursor 
GO 

if not exists(select * from ##a12 where wil<>'') 
begin 

배치가 두 번째 실행에 구문 분석됩니다 때 wil 열이 삭제 되었기 때문에 이런 일이하고 파서가 다시 될 것으로 인식하지 않습니다 임시 테이블이 제거되고 다시 작성 될 때 추가됩니다.

GO을 추가하면 배치를 두 개로 분할하고 두 번째 배치가 실행될 때 wil 열이 다시 존재하게됩니다.

+0

그래, 그걸 다시 실행할 수 있다는 사실을 고쳤지만, @p 값이 같을 때만. 이 값이 변경되면 (사용자 입력에 따라 다름) 오류가 다시 발생하고 수동으로 테이블을 삭제해야합니다. – JohnZ

+0

흠 ...'@ p '의 값으로 인해 구문 분석 오류가 발생하는 이유를 알 수 없습니다. @ AaronBertrand의 조언을 따르고 일괄 처리가 끝날 때 임시 테이블을 삭제합니다. –

관련 문제