user1737619
user1737619

Reputation: 247

What is the usage of '@@' in SQL Server

What is the usage of @@ in SQL Server?

Upvotes: 14

Views: 20151

Answers (3)

AHiggins
AHiggins

Reputation: 7219

According to MSDN, the correct name for these is system functions.

The naming confusion (global variable, system function, global function) stems from different terminology used throughout SQL Server's history. From the MSDN Transact-SQL Variables article:

The names of some Transact-SQL system functions begin with two at signs (@@). Although in earlier versions of Microsoft SQL Server, the @@functions are referred to as global variables, they are not variables and do not have the same behaviors as variables. The @@functions are system functions, and their syntax usage follows the rules for functions.

Thus, two 'at' symbols (@@) are used to denote some system functions. The use of the phrase "global variable" was deprecated (though you will still see some people use it), most likely because in the programming world a global variable is a single value that is visible everywhere, and as already pointed out that isn't what is happening here (e.g., @@IDENTITY).

Further confusion is likely caused by the way temporary tables are named. A single hash sign prefixing a table name indicates a locally-scoped temporary table (e.g., #MyLocalTable), much like a single at symbol indicates a locally-scoped variable (e.g., @MyLocalVariable). Adding a second hash sign to a temporary table makes it globally-scoped (e.g., ##MyGlobalTable), but trying to add two at symbols to a variable does not produce the same effect.

Upvotes: 9

mohan111
mohan111

Reputation: 8865

@ is for a local variable

@@ is for a global variable or function.

There are several standard global variables or functions, e.g.: @@IDENTITY, @@ROWCOUNT, @@TRANCOUNT

Upvotes: 4

paul
paul

Reputation: 22001

@@ is used to prefix internal statistical and metadata functions that return information about how the SQL Server is configured, not specific to any particular database.

For example, these include the number of connections made to the database (@@CONNECTIONS), and the first day of the week (@@DATEFIRST)

https://msdn.microsoft.com/en-us/library/ms173823.aspx

https://msdn.microsoft.com/en-us/library/ms177520.aspx

Upvotes: 2

Related Questions