Ok this may be idiot question but I want to know why does the update statement gives divided by 0 operation even thought there is no row in the table. Does it execute the set function first and then try to apply to the where clause.
CREATE TABLE #tmp
(
Id INT,
Amount money
)
UPDATE t
SET t.Amount=0/0
FROM #tmp AS t
WHERE t.Id=2
DROP TABLE #tmp
4条答案
按热度按时间6qqygrtg1#
If you try it like this you will not get an error :
You get the error because your where clause forced a search in your table, and it will peform the set operation also at the same time
c9qzyr3d2#
In SQL, you tell the system what you want, not how to do it.
Integer division is deterministic and you have an expression (
0/0
) that has no dependency on row values. It therefore seems perfectly reasonable to extract the expression0/0
out, perform that calculation first, and then use the result of that expression as it actually scans through the table. So you get an error because it's not even started any work of processing the table.In Guido's answer , the optimizer found a
WHERE
clause that meant that it could already tell that the query would affect zero rows (without needing to access any statistics about the table) and so it was able to optimize its work even further.gudnpqoy3#
why does the update statement gives divided by 0 operation even thought there is no row in the table. Does it execute the set function first and then try to apply to the where clause.
Question is not about dived by 0,rather it is about when does any error occur.
BTW query easily pass through "Parse stage" and hand it is handed over to "Optimization" stage.
Example,
declare @table table(id int identity(1,1),PhoneNumber int)
If you see "
Estimated Execution plan (EEP)
" then there is no error.that means it passes through Parse Stage and also EEP is created.when we try to view
Actual Execution Plan (AEP)
then we get relevant error or divided by 0 error in this example.Here again there is no problem in getting EEP. If we click on AEP then 1 rows is return and get error in 2nd row.
This prove that divided by 0/0 condition are executed when it meet certain condition.
Here again there is no problem in getting EEP. If we click on AEP then 1 rows is return and get error in 2nd row.
This prove that divided by 0/0 condition are executed when it meet certain condition.
3pmvbmvn4#
Because the query will be parsed before SQL Server attempts to execute it. At that point it will throw an error because the command is unexecutable.