SQL Server 2008 - Find out on which data row the error (divide by zero) appeared
i have a query running on a large amount of data in SSMS. After about 20 minutes of execution, the query completes with an error 'Divide b开发者_StackOverflow社区y zero' (some results are already returned).
It would be helpful to know on which data the error appeared, i.e. find the id/row nuber on which the error can be reproduced.
The query itself is rather complicated, so i am not going to post it; the question is more technical - is there a log somewhere, or another way to find the problematic row(s)?
Select
*,
Dividend / NULLIF(Divisor, 0) as NullIfBollixedNow
FROM
MyTable
-- to isolate the rows(s). Without this, you get NULL instead of error in the output
WHERE
Dividend / NULLIF(Divisor, 0) IS NULL
In SQL Server there isn't an easy way to do this as Ordering or other functions may affect the row order after the Put in some exception handling into your procedure using case statements and output an exception value on the broken rows. Something like this
If you query was
Select MyFirstField / MySecondField as TestMath
From myTblOfFacts
Select
Case when MySecondField > 0 then MyFirstField / MySecondField
Else -1
End as TestMath
From myTblOfFacts
THis would return -1 for the rows with an issue.
精彩评论