2012-12-06 4 views
0

가능한 중복 나누어 피하는 방법 :
How to avoid the “divide by zero” error in SQL?제로 오류로

을 나는 제로 오류로 나눈 SQL 서버에 대한 문제가있다. 때때로이 오류와보기는 해당 행을 삭제할 때까지 차단됩니다. 이 문제를 어떻게 피할 수 있습니까? 도와 주시겠습니까? 감사합니다. .

CREATE VIEW Acq 
AS 
SELECT 
     ac_id 
     ,[Company] 
     ,No 
     ,[ContractID] 
     ,[Seller] 
     ,[AcquistionDate] 
     ,[Village] 
     ,[Commune] 
     ,[Area] 
     ,[PlotArea] 
     ,[FieldNo] 
     ,[Topo1] 
     ,[Topo2] 
     ,[Topo3] 
     ,[Topo4] 
     ,[Topo5] 
     ,[TotalAreaSqm] 
     ,[OwnershipTitle] 
     ,[CadastralNO] 
     ,[Type] 
     ,[Price] 
     ,[NotaryCosts] 
     ,[LandTax] 
     ,[OtherTaxes] 
     ,[AgentFee] 
     ,[CadastralFee] 
     ,[TabulationFee] 
     ,[CertSarcini] 
     ,[ProcuraNO] 
     ,cast((isnull(price,0)+isnull(notarycosts,0)+isnull(landtax,0)+isnull(othertaxes,0)+isnull(agentfee,0)+isnull(cadastralfee,0)+isnull(tabulationfee,0)+isnull(certsarcini,0)) as decimal(12,4)) as TotalCosts 
     ,cast((isnull(price,0)+isnull(notarycosts,0)+isnull(landtax,0)+isnull(othertaxes,0)+isnull(agentfee,0)+isnull(cadastralfee,0)+isnull(tabulationfee,0)+isnull(certsarcini,0))/(TotalAreaSqm/10000) as decimal(12,4)) as RonPerHa 
     ,cast((isnull(price,0)+isnull(notarycosts,0)+isnull(landtax,0)+isnull(othertaxes,0)+isnull(agentfee,0)+isnull(cadastralfee,0)+isnull(tabulationfee,0)+isnull(certsarcini,0))/(TotalAreaSqm/10000*FixHist) as decimal(12,4)) as EurPerHa 
     ,[DeclImpunere] 
     ,[FixHist] 
     ,cast((isnull(price,0)+isnull(notarycosts,0)+isnull(landtax,0)+isnull(othertaxes,0)+isnull(agentfee,0)+isnull(cadastralfee,0)+isnull(tabulationfee,0)+isnull(certsarcini,0))/FixHist as decimal(12,4)) as EurHist 
     ,[LandStatus] 


FROM  tblAcq 

답변

3

하는 (nullif(TotalAreaSqm,0)/10000)

(TotalAreaSqm/10000) 같은 표현을 바꾸기 이렇게하면 결과가 0으로 나누기의 경우 null 싶습니다 가정합니다. nullif에 대한 설명은 here 문서를 참조하십시오.

+0

고마워, 작동 중! – user1820705

1
CASE (TotalAreaSQM/1000 <> 0) THEN 'do work' ELSE 'dont do it and pass the 0 or what ever' 

당신이 쓸 수에 simmilar 물건 Gazilion.

+0

도움 주셔서 감사합니다. – user1820705