SQL - @@IDLE Function



The SQL @@IDLE statistical function is used to retrieve the idle time. It returns the total idle time that SQL server has been idle since it was last started(or began).

Note − If the time returned by the @@CPU_BUSY function exceeds the 49 days of cumulative CPU time, then you may get an arithmetic overflow warning.In that case, the value of @IDLE variables are accurate.

Syntax

Following is the syntax of the SQL @@IDLE function −

@@IDLE

Return type

The return type of this function is an INTEGER.

Parameters

  • It does not accept any parameters.

Return value

This function returns the total idle time of this SQL server.

Example

In the following example,we are using the SQL @@IDLE function to retrieve the total idle time of this SQL server since its last start.

SELECT @@IDLE AS Total_idle_time;

Output

Following is the output of the above query −

+-------------------+
| Total_idle_time   |
+-------------------+
| 241867257         |
+-------------------+

Example

The following is another example of the SQL @@IDLE function. You can also use the GETDATE() function along with this function to retrieve the total idle time until today of this SQL server since it last began.

SELECT @@IDLE AS Total_idle_time, GETDATE() AS Curr_date;

Output

On executing the above program, it will produce the following output −

+-----------------+-------------------------+
| Total_idle_time | Curr_date               |
+-----------------+-------------------------+
| 241896780       | 2023-02-28 15:05:51.770 |
+-----------------+-------------------------+

Example

In this example, we are using the SQL @@IDLE function to retrieve the total idle time of this SQL server, and we are using the CAST and @@TIMETICKS functions to print idle time in a microsecond.

SELECT @@IDLE AS Total_idle_time, GETDATE() AS Curr_date, @@IDLE *CAST(@@TIMETICKS AS float) AS Time_in_microsecond;

Output

The above SQL query produces the following output −

+-----------------+-------------------------+---------------------+
| Total_idle_time | Curr_date               | Time_in_microsecond |
+-----------------+-------------------------+---------------------+
| 242242169       | 2023-02-28 15:28:56.847 | 7570067781250       |
+-----------------+-------------------------+---------------------+
sql-statistical-functions.htm
Advertisements