Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the consequences of declaring a SQL Server Variable with two (or more) '@' symbols?

Ordinarily, I would declare a variable

declare @ConType int;

Or something akin to this.

Recently in a code review I encountered a decalration using a double '@' (like the built in @@rowcount, for example), i.e.

declare @@ConType int;

I notice that one can stupulate any (reasonable) number of '@':

declare @@@@ConType int;

And the variable should function ok. So, the following should work:

declare @@@@ConType int;
set @@@@ConType = 1;
select @@@@ConType;

Obvoiusly, the above is a little stupid, but my question is really whether there is any significance in declaring variables in this way? Are there any side-effects? Should we avoid doing so?

like image 727
James Wiseman Avatar asked Jan 27 '11 14:01

James Wiseman


People also ask

Can you DECLARE multiple variables in SQL?

Regarding feature of SQL Server where multiple variable can be declared in one statement, it is absolutely possible to do. From above example it is clear that multiple variables can be declared in one statement.

Which symbol is used to DECLARE a variable in a batch in SQL Server?

Update: Declaring any variable with @@ prefix (except the system-defined) is actually a local variable. Show activity on this post. Local variables are declared by the user and can be used in procedures or in batches of SQL statements to hold information.

How do you DECLARE a variable in SQL and assign a value?

To assign a value to a variable, use the SET statement. This is the preferred method of assigning a value to a variable. A variable can also have a value assigned by being referenced in the select list of a SELECT statement.

How DECLARE variable in SQL Server?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.


2 Answers

The first character of a variable name has to be an at sign ('@'). Any at signs after that do not have any particular significance and are treated just like any other character.

However, you should avoid declaring variables that begin with a double at sign ('@@') because, in the words of MSDN:

Some Transact-SQL functions have names that start with double at signs (@@). To avoid confusion with these functions, you should not use names that start with @@.

Of course, this means that variable names beginning with three or more at signs should also not be used.

I guess it's not exactly wrong to use at signs later in the variable name if the second character is not an at sign, but it just looks confusing, so it's probably not a good idea either.

like image 100
Martin B Avatar answered Nov 03 '22 00:11

Martin B


From MSDN:

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.

[EDIT] To clarify, SQL Server will let you put however many at signs you want at the front of your variable names, but technically @@ objects are not variables at all. They behave like that though they look like system functions. Some people try to use the @@ to designate a global variable, but that doesn't work; there is no way for you to create a global variable. This behavior is typically just left over from using earlier versions of SQL Server.

like image 20
schellack Avatar answered Nov 02 '22 23:11

schellack