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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With