2013-01-02 1 views
0

은 : 난 내 선택 중 하나를 실행하면nvarchar 값 'some name을 데이터 형식 int로 변환 할 때 변환하지 못했습니다. 나는이 오류 메시지가 무엇입니까 내 전체 쿼리를 실행하면

------------------------------------ 
--POPULATE SECTIONAAVGTAT----------- 
------------------------------------ 
;with SpecimenDetail as (
select 
s.* 
,a.[mlis id] 
,a.director 
,a.rm 
,a.rep 
,a.css 
,a.css2 
,dbo.networkdays(s.[date received],GETDATE())-1 [Days On Hold] 
,(case when dbo.networkdays(s.[date received],GETDATE())-1>=7 then '7+' 
    when dbo.networkdays(s.[date received],GETDATE())-1 in (5,6) then '5-6' 
    when dbo.networkdays(s.[date received],GETDATE())-1 in (3,4) then '3-4' 
    when dbo.networkdays(s.[date received],GETDATE())-1 in (1,2) then '1-2' 
    when dbo.networkdays(s.[date received],GETDATE())-1=0 then '0' 
    else 'na' 
end) as [Days on Hold Group] 
from specimendetailtmp s 
left join sectionaalignment a 
on s.[mlis practice id]=a.[mlis id] 
where s.[date distributed] is not null 
and s.[date received]>='20121112' 
and s.sectiona='not marked' 
) 


, 

AvgTAT as 
(
select 'director' emptype,director employee, cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1 as float)), 3, 1) as decimal(10,1)) AvgTAT,month(s.[date received]) month from SpecimenDetail s 
where s.[date distributed] is not null 
group by director,month(s.[date received]) 


union all 

select 'rm' emptype,rm employee, cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1 as float)), 3, 1) as decimal(10,1))AvgTAT,month(s.[date received]) month from SpecimenDetail s 
where s.[date distributed] is not null 
group by rm,month(s.[date received]) 

union all 

select 'rep' emptype,rep employee,cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1 as float)), 3, 1) as decimal(10,1)) AvgTAT,month(s.[date received]) month from SpecimenDetail s 
where s.[date distributed] is not null 
group by rep,month(s.[date received]) 

union all 

select 'css' emptype,css employee,cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1 as float)), 3, 1) as decimal(10,1)) AvgTAT,month(s.[date received]) month from SpecimenDetail s 
where s.[date distributed] is not null 
group by css,month(s.[date received]) 

union all 

select 'css2' emptype,css2 employee,cast(round(AVG(cast (dbo.networkdays(s.[date received],s.[date distributed])-1 as float)), 3, 1) as decimal(10,1)) AvgTAT,month(s.[date received]) month from SpecimenDetail s 
where s.[date distributed] is not null 
group by css2,month(s.[date received]) 
) 

select * from avgtat 

그러나, 모든 작동합니다!

왜 전체 스크립트를 실행할 때만이 오류가 발생합니까?

답변

2

은 내가 UNION ALL 세그먼트에서 하나 또는 SELECT 조항의 이상이 암시 int로 변환 결과를 생산하고 있다고 생각한다. 그리고 다른 SELECT 절은 nvarchar 인 결과를 반환합니다. 암시 적 변환은 결과의 열 값이 모두 정수일 때 발생할 수 있습니다.

어떤 열이 문제의 원인인지 알지 못하는 경우 각 SELECT의 결과를 검토하고 공통 패턴이 있는지 확인해야합니다 (예 : 모든 값이 nvarchar 열에 정수 임). 그런 다음 알면 문제를 생성하는 열을 CAST 또는 CONVERT을 사용하여 값을 원하는 데이터 형식으로 명시 적으로 캐스팅합니다.

+0

정말 고마워요! 하지만 난 심지어 그것을 참조하는 열을 몰라! –

+1

사용되지 않는 공통 테이블 표현식에서 열을 제거하여 시작할 수 있습니다. 그리고 오류가 사라질 때까지'UNION ALL' 쿼리에서 열을 임시로 제거하십시오. 제거 된 마지막 열은 문제를 일으키는 열입니다. – bobs

관련 문제