|
What is the difference between the following declarations: DECLARE @id bigint and DECLARE @@id bigint
(comments are locked)
|
|
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. the only difference being that there are 2 different variables being declared :)
May 26 '10 at 11:22 AM
Kev Riley ♦♦
(comments are locked)
|
|
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 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)
|

