x
login about faq Site discussion (meta-askssc)

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 '10 at 10:59 AM in Default

Fayu gravatar image

Fayu
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 '10 at 11:10 AM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.2k 56 63 87

the only difference being that there are 2 different variables being declared :)

May 26 '10 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 '10 at 11:08 AM

Fatherjack gravatar image

Fatherjack ♦♦
38.8k 55 69 104

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 '10 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 '10 at 11:28 AM Fayu

So now my question is: Is it bad practice to use @@?

May 26 '10 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 '10 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 '10 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.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x599
x272

asked: May 26 '10 at 10:59 AM

Seen: 2168 times

Last Updated: May 26 '10 at 10:59 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.