Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL WHERE EXISTS is masking errors in Subquery

I am running a query to update a flag in a table that relies on checking that values exist in a subquery, but when the subquery gets an error (divide by zero), the EXISTS statement just sees it as a returned row and carries on with the update.

e.g.

Update xxxx
Set Flagfield=1 
FROM xxxx
WHERE
EXISTS (
Select * FROM yyyy Inner join xxxx on xxx.ID = yyyy.id
WHERE yyyy.int1 / yyyy.int2 > 1)

Has anyone else experienced this behaviour and can it be anticipated?

like image 310
Rich Avatar asked Aug 09 '26 20:08

Rich


1 Answers

If I understand your question correctly, then you can try the below code, just add the check for int2 in the where statement.

Update xxxx
Set Flagfield=1 
FROM xxxx
WHERE
EXISTS (
Select * FROM yyyy Inner join xxxx on xxx.ID = yyyy.id
WHERE (yyyy.int2 IS NOT NULL AND yyyy.int2 <> 0) AND (yyyy.int1 / yyyy.int2 > 1))
like image 77
AhmedBinGamal Avatar answered Aug 11 '26 10:08

AhmedBinGamal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!