14

What is the usage of @@ in SQL Server?

shA.t
  • 16,580
  • 5
  • 54
  • 111
user1737619
  • 247
  • 1
  • 2
  • 15

3 Answers3

9

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.

Community
  • 1
  • 1
AHiggins
  • 7,029
  • 6
  • 36
  • 54
4

@ 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

MikeMB
  • 20,029
  • 9
  • 57
  • 102
mohan111
  • 8,633
  • 4
  • 28
  • 55
  • So you have a source for this? Because you are suggesting that `@@IDENTITY` is global, meaning that all users see the same value. So if I do an `INSERT` and then `SELECT @@IDENTITY` I might get the identity of an insert done by a completely different user? – asontu Apr 16 '15 at 10:22
  • 1
    @funkwurm, which is why you use scope_identity https://msdn.microsoft.com/en-us/library/ms190315.aspx though both limited by session, but still it's sort of global – pinkfloydx33 Apr 16 '15 at 10:39
2

@@ 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

paul
  • 21,653
  • 1
  • 53
  • 54