Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2002
    Location
    Sunnyvale, CA USA
    Posts
    78

    Unanswered: Two @'s or one for T-SQL variables?

    Can anyone tell me the difference between having two at-signs at the front of a variblename and having only one?

    e.g. Here are two variables:

    declare @fred varchar(10)
    set @fred='hello'

    declare @@fred varchar(10)
    set @@fred='goodbye'

    select @fred
    select @@fred

    ----------
    hello
    (1 row(s) affected)

    ----------
    goodbye
    (1 row(s) affected)

    So clearly they are two different variables. Are there any other differences in their properties? Sometimes BOL uses one, sometimes two. Any ideas why?

    Thanks.

    (I know that some special, predefined 2-at variables exist, but are they fundamentally any different from 1-at variables?)

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    I don't have Books On Line in front of me, but I think that indicates that they are global, just like one # sign in front of a temporary table name means it is available only to the current connection, but two # signs make it available to all connections.

    blindman

  3. #3
    Join Date
    Aug 2003
    Location
    Delft, The Netherlands (EU)
    Posts
    447
    Originally posted by blindman
    I don't have Books On Line in front of me, but I think that indicates that they are global, just like one # sign in front of a temporary table name means it is available only to the current connection, but two # signs make it available to all connections.

    blindman
    That's right for tables, but variables can't be declared globally. SQL Server has some system-defined variables like @@CONNECTIONS, but a user can't do the same. However, SQL Server does not prevent a user to declare @@Fred, even @@@Fred is possible (see DECLARE @local_variable (T-SQL) syntax), but the common approach is to give your variables a name without a second @.
    Make everything as simple as possible, but not simpler! - A. Einstein
    DB Problems? DB Explorer, BTrieve Re-engineering, DB Conversions & ETL? Conversion Tool

  4. #4
    Join Date
    Feb 2003
    Location
    Milano, Italy
    Posts
    80
    To be more precise, @@ objects are not variables at all, though they behave like that. They are a particulare system functions.

    In SQL Server there is NO WAY to create global variables...they do not exists! :-)

    The object nearest to a global variable is a global temp table with one row and one column
    Davide Mauri
    http://www.davidemauri.it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •