에서 다음 번 쿼리 이상을 실행할 수 없습니다, 나는이 내가 그것을 처음 실행할 때 작동하지만, 내가 다시 실행하려고하면, 나는 오류를 얻을이 쿼리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 문을 사용했지만 어떤 이유로 그것을 지금 사용하지 않습니다.
## 글로벌 임시 테이블을 사용하는 이유는 무엇입니까? 왜 # 로컬이 아닌가? 왜 끝나면 명시 적으로 테이블을 떨어 뜨리지 않는거야? –
동적 SQL은 외부와 통신하기 위해 ##가 필요합니다. – JohnZ
@JohnZ 동적 SQL 내에서 'EXEC()'보다 더 높은 범위에서 생성하기 때문에 여전히 로컬 임시 테이블을 참조 할 수 있어야합니다. –