SQL - @@CONNECTIONS Function



The SQL @@CONNECTIONS statistical function is used to retrieve the number of connection attempts. In SQL Server this function returns the number of attempted connections since the SQL server was last time started. It includes both successful and unsuccessful(or failed) connection attempts.

Syntax

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

@@CONNECTIONS    

Return type

The return type of this function is an INTEGER.

Parameters

  • It does not accept any parameters.

Return value

It returns number of attempted connections.

Example

In the following example,we are using the SQL @@CONNECTIONS function to retrieve the number of connection attempts including both successful and unsuccessful.

SELECT [ConnectionAttempts] = @@CONNECTIONS;

Output

Following is the output of the above query −

+--------------------+
| ConnectionAttempts |
+--------------------+
| 71072              |
+--------------------+

Example

The following is another example of the SQL @@connections function. You can also use the GETDATE()function along with this function to retrieve the number of connection attempts until today, including both successful and unsuccessful.

SELECT GETDATE() AS 'Current_Date', @@CONNECTIONS AS 'Login Attempts';

Output

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

+-------------------------+----------------+
| Current_Date            | Login Attempts |
+-------------------------+----------------+
| 2023-02-28 11:20:10.743 | 71005          |
+-------------------------+----------------+

Example

In this example, we are using the SQL @@CONNECTIONS and @@MAX_CONNECTIONS functions to retrieve the number of login connection attempts, including both successful and unsuccessful, and the number of max_allowed attempts.

SELECT [ConnectionAttempts] = @@CONNECTIONS,
[Max_allowed] = @@MAX_CONNECTIONS;

Output

The above statement produces the following output −

+--------------------+----------------+
| ConnectionAttempts | Max_allowed    |
+--------------------+----------------+
| 71177              | 32767          |
+--------------------+----------------+
sql-statistical-functions.htm
Advertisements