Sql Server - Difference between @id and @@id

What is the difference between the following declarations:

DECLARE @id bigint and DECLARE @@id bigint

more ▼

asked May 26, 2010 at 10:59 AM in Default

Fayu gravatar image

35 4 4 5

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered May 26, 2010 at 11:10 AM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.5k 61 65 87

the only difference being that there are 2 different variables being declared :)
May 26, 2010 at 11:22 AM Kev Riley ♦♦
(comments are locked)
10|1200 characters needed characters left

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

more ▼

answered May 26, 2010 at 11:08 AM

Fatherjack gravatar image

Fatherjack ♦♦
42.8k 75 79 108

Ha - a few seconds. Are you sure about @@ being a global variable? Because I'm pretty sure there was never such a thing. Perfectly prepared to eat my hat, but I don't think there was ever a global variable concept in SQL Server. There are global temporary objects (##) though.
May 26, 2010 at 11:12 AM Matt Whitfield ♦♦

This is from the link above:

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.
May 26, 2010 at 11:28 AM Fayu
So now my question is: Is it bad practice to use @@?
May 26, 2010 at 11:29 AM Fayu
@Matt - no not certain at all. Wrote my answer, snagged BOL and pasted link. May be some difference between my opinion and theirs... :-/ I could well have this confused with # and ## ... will read later, currently busy as on leave for 2 days.
May 26, 2010 at 11:41 AM Fatherjack ♦♦
Yep - what that is saying is that the @@ system functions used to be called 'global variables' - but they weren't actually variables at all...
May 26, 2010 at 12:26 PM Matt Whitfield ♦♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: May 26, 2010 at 10:59 AM

Seen: 3976 times

Last Updated: May 26, 2010 at 10:59 AM