Search code examples
sql-serversql-server-2008-r2

Difference between @@ and @ in SQL Server 2008 R2


I am using SQL Server 2008 R2.

My question is What is the difference between declaring variables by using @ and @@?

In SQL Server 2008 R2:

Example:

Syntax 1:

Declare @a int = 1

select @a;

Syntax 2:

Declare @@a int = 1

select @@a;

Both does the same job here. Then why should we use double @@?


Solution

  • 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. However, if you match a system function which has the same name as your variable, then the query will not compile.

    In previous versions of SQL Server @ described a local variable and @@ describe a global variable. Now (SQL 2008) @@ is used for a function name so @@ shouldnt be used as a parameter name.

    Books OnLine ref http://msdn.microsoft.com/en-us/library/ms187953.aspx

    if any other problem comment me :)