Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the usage of @@ in SQL Server?

like image 416
user1737619 Avatar asked Apr 16 '15 10:04

user1737619


People also ask

What is the use of %s in SQL?

%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders. $sql = sprintf($sql, "Test"); This would replace %s with the string "Test".

What is @@ options in SQL Server?

The @@OPTIONS function returns a bitmap of the options, converted to a base 10 (decimal) integer. The bit settings are stored in the locations described in a table in the topic Configure the user options Server Configuration Option.

What is in operator in SQL Server?

The Microsoft SQL Server IN operator is used to replace a group of arguments using the = operator that are combined with an OR in for SELECT, UPDATE or DELETE statement. It can make code easier to read and understand.

What is the difference between and @@ in SQL Server?

There is no difference. The rules for variables state that they start with an '@' character and follow the rules for identifiers. Since '@' is a valid identifier character, you can have as many as you like at the start of your variable name.


3 Answers

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

like image 56
paul Avatar answered Sep 19 '22 10:09

paul


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.

like image 22
AHiggins Avatar answered Sep 20 '22 10:09

AHiggins


@ 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

like image 34
mohan111 Avatar answered Sep 19 '22 10:09

mohan111