Global Variables in MS SQL Server

19 Aug, 2020
Global variables are pre-defined system variables. It starts with @@. It provides information about the present user environment for SQL Server. SQL Server provides multiple global variables, which are very effective to use in Transact-SQL. The following are some frequently used global variables –
@@SERVERNAME
@@CONNECTIONS
@@MAX_CONNECTIONS
@@CPU_BUSY
@@ERROR
@@IDLE
@@LANGUAGE
@@TRANCOUNT
@@VERSION
These are explained as following below.
@@SERVERNAME :This is used to find the name of the machine/computer on which SQL Server is running.Example –Select @@servernameOutput –SERVERXX\CTRXREST@@CONNECTIONS :This is used to find number of logins or attempted logins since SQL Server was last started.Example –Select @@connectionsOutput –59846824@@MAX_CONNECTIONS :This is used to find the maximum number of simultaneous connections that can be made with SQL Server or instance in this computer environment.Example –select @@max_connectionsOutput –32767@@CPU_BUSY :This is used to find the amount of time, in microseconds, that the CPU has spent doing SQL Server work since the last time SQL Server was running.Example –Select @@cpu_busyOutput –887468@@ERROR :This is used to check the error status (succeeded or failed) of the most recently executed statement. It contains Zero (0) if the previous transaction succeeded, else, it contains the last error number generated by the system.Example –Select @@errorOutput –0@@IDLE :The amount of time, in microseconds, that SQL Server has been idle since it was last started.Example –Select @@idleOutput –123691249@@LANGUAGE :This is used to find the name of the language that is currently used by the SQL Server.Example –Select @@languageOutput –us_english@@TRANCOUNT :This is used to count the number of open transactions in the current session.Example –Select @@trancountOutput –0@@VERSION :This is used to find the current version of the SQL Server Software.Example –Select @@versionOutput –Microsoft SQL Server 2014 (SP3-CU-GDR) (KB4535288) - 12.0.6372.1 (X64)
Dec 12 2019 15:14:11
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
@@SERVERNAME :This is used to find the name of the machine/computer on which SQL Server is running.Example –Select @@servernameOutput –SERVERXX\CTRXREST
Select @@servername
Output –
SERVERXX\CTRXREST
@@CONNECTIONS :This is used to find number of logins or attempted logins since SQL Server was last started.Example –Select @@connectionsOutput –59846824
Select @@connections
Output –
59846824
@@MAX_CONNECTIONS :This is used to find the maximum number of simultaneous connections that can be made with SQL Server or instance in this computer environment.Example –select @@max_connectionsOutput –32767
select @@max_connections
Output –
32767
@@CPU_BUSY :This is used to find the amount of time, in microseconds, that the CPU has spent doing SQL Server work since the last time SQL Server was running.Example –Select @@cpu_busyOutput –887468
Select @@cpu_busy
Output –
887468
@@ERROR :This is used to check the error status (succeeded or failed) of the most recently executed statement. It contains Zero (0) if the previous transaction succeeded, else, it contains the last error number generated by the system.Example –Select @@errorOutput –0
Select @@error
Output –
0
@@IDLE :The amount of time, in microseconds, that SQL Server has been idle since it was last started.Example –Select @@idleOutput –123691249
Select @@idle
Output –
123691249
@@LANGUAGE :This is used to find the name of the language that is currently used by the SQL Server.Example –Select @@languageOutput –us_english
Select @@language
Output –
us_english
@@TRANCOUNT :This is used to count the number of open transactions in the current session.Example –Select @@trancountOutput –0
Select @@trancount
Output –
0
@@VERSION :This is used to find the current version of the SQL Server Software.Example –Select @@versionOutput –Microsoft SQL Server 2014 (SP3-CU-GDR) (KB4535288) - 12.0.6372.1 (X64)
Dec 12 2019 15:14:11
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
Select @@version
Output –
Microsoft SQL Server 2014 (SP3-CU-GDR) (KB4535288) - 12.0.6372.1 (X64)
Dec 12 2019 15:14:11
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
DBMS-SQL
SQL-Server
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Update Multiple Columns in Single Update Statement in SQL?
Window functions in SQL
What is Temporary Table in SQL?
SQL | Sub queries in From Clause
SQL using Python
RANK() Function in SQL Server
SQL Query to Find the Name of a Person Whose Name Starts with Specific Letter
SQL Query to Convert VARCHAR to INT
SQL Query to Compare Two Dates
How to Write a SQL Query For a Specific Date Range and Date Time?