Solved

Divide by zero error encountered.

Posted on 2016-09-29
2
28 Views
Last Modified: 2016-09-29
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
0
Comment
Question by:bibi92
2 Comments
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
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],
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
>Why?
Because the denominator in a division operation is a zero.  Why else?

The usual T-SQL way of handling divide by zero errors goes like this..
CASE WHEN denominator = 0 THEN 0 ELSE numerator / denominator END as column_name

Open in new window

So take the above template and apply it to the three places you have division operators in your query.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now