Avatar of bibi92
bibi92
Flag for France asked on

Divide by zero error encountered.

Hello,

When I execute the following query, the error Divide by zero error encountered. is generated :
SELECT Object_name(qt.objectid),
       qs.execution_count                                                 AS
       [Execution Count],
       qs.execution_count / Datediff(second, qs.creation_time, Getdate()) AS
       [Calls/Second],
       qs.total_worker_time / qs.execution_count                          AS
       [AvgWorkerTime],
       qs.total_worker_time                                               AS
       [TotalWorkerTime],
       qs.total_elapsed_time / qs.execution_count                         AS
       [AvgElapsedTime],
       qs.max_logical_reads,
       qs.max_logical_writes,
       qs.total_physical_reads,
       Datediff(minute, qs.creation_time, Getdate())                      AS
       [Age in Cache]
FROM   sys.dm_exec_query_stats AS qs
       CROSS apply sys.Dm_exec_sql_text(qs.[sql_handle]) AS qt
WHERE  qt.[dbid] = Db_id()
ORDER  BY qs.execution_count DESC
OPTION (recompile);

Why?

Thanks
Microsoft SQL ServerMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
Jim Horn

8/22/2022 - Mon
lcohan

Please check all divisions and make sure none of those columns or record set listed below can return NULL. Add ISNULL() around them or COALESCE()
...
 / Datediff(second, qs.creation_time, Getdate()) AS  [Calls/Second],
...
/ qs.execution_count                          AS ...
....
/ qs.execution_count                         AS [AvgElapsedTime],
ASKER CERTIFIED SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy