Reputation: 7260
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 @@?
Upvotes: 1
Views: 6545
Reputation: 1
The other user is not correct, they are not EXACTLY the same.
CREATE PROCEDURE [dbo].[JustWriteLetter]
@@letter1 nvarchar(1) = 'A',
@letter2 nvarchar(1) = 'B'
AS
BEGIN
SELECT @@letter1,@letter2
END;
When you run this code and refresh your parameters... you will get this.Insightful message about how the first param has no default, even though it does.
But when you run the procedure with no params...
It acts in an unexpected, expected way.
So when you use @@ with a default parameter, SQL is just... well, going to lie to you about having a default parameter... it's a bug that's also a feature.
Upvotes: 0
Reputation: 539
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 :)
Upvotes: 7