question

Fayu avatar image
Fayu asked

Sql Server - Difference between @id and @@id

What is the difference between the following declarations:

DECLARE @id bigint and DECLARE @@id bigint

sqlsql-server
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image
Fatherjack answered

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

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Yep - what that is saying is that the @@ system functions used to be called 'global variables' - but they weren't actually variables at all...
1 Like 1 ·
Matt Whitfield avatar image
Matt Whitfield answered

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.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.