locked
What does @ /@@ mean? RRS feed

  • Question

  • I'm trying to find a clear explanation for what @ or @@ mean in T-SQL as I can't figure this out...
    Sunday, December 13, 2009 5:46 PM

Answers

  • Local variable names are prefixed with @. Some T-SQL system function names begin with @@ (sometimes they are called global variables but in fact they are functions).
    Plamen Ratchev
    • Marked as answer by meridius10 Tuesday, December 15, 2009 9:59 PM
    Sunday, December 13, 2009 5:55 PM
  • No, the @variable is just like a variable in any other language, say, in C# you would declare new int variable

    int myIntVar; //you don't need to initialize it right away

    In T-SQL the analogue would be

    declare @myIntVar int

    In SQL Server 2008 you can declare and initialize in one statement:

    declare @myIntVar = 5

    Please read the link I gave you earlier to understand how to declare variables in T-SQL and how to declare table variables.

    You don't need CREATE table before running a declare - they serve different purposes.

    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    • Proposed as answer by Murty Addanki Tuesday, December 15, 2009 4:30 AM
    • Marked as answer by meridius10 Tuesday, December 15, 2009 9:56 PM
    Tuesday, December 15, 2009 3:24 AM

All replies

  • @ refers to the local variables you create with DECLARE statement, for example, declare @TotalCount int, @StartDate datetime

    The parameters in functions and stored procedures also have @ in front of them.

    @@ refers to system defined functions, such as @@ROWCOUNT, @@Error, for example.

    See http://msdn.microsoft.com/en-us/library/ms187953.aspx for more info.



    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    • Proposed as answer by onpnt Sunday, December 13, 2009 5:55 PM
    Sunday, December 13, 2009 5:53 PM
  • Local variable names are prefixed with @. Some T-SQL system function names begin with @@ (sometimes they are called global variables but in fact they are functions).
    Plamen Ratchev
    • Marked as answer by meridius10 Tuesday, December 15, 2009 9:59 PM
    Sunday, December 13, 2009 5:55 PM
  • This may be stating the obvious (or not) but do I need to CREATE TABLE with column names before running a DECLARE statement?

    I am guessing that a local variable refers to a column name and property within the database. So is global variable (in the old sense) columns outside the current db.?

    It's going to take me a little time to get my head around this...

    Tuesday, December 15, 2009 1:28 AM
  • No, the @variable is just like a variable in any other language, say, in C# you would declare new int variable

    int myIntVar; //you don't need to initialize it right away

    In T-SQL the analogue would be

    declare @myIntVar int

    In SQL Server 2008 you can declare and initialize in one statement:

    declare @myIntVar = 5

    Please read the link I gave you earlier to understand how to declare variables in T-SQL and how to declare table variables.

    You don't need CREATE table before running a declare - they serve different purposes.

    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    • Proposed as answer by Murty Addanki Tuesday, December 15, 2009 4:30 AM
    • Marked as answer by meridius10 Tuesday, December 15, 2009 9:56 PM
    Tuesday, December 15, 2009 3:24 AM
  •  Hi,

    Identifier starting with @ you can use in declaring the variable(local variables)
    but   identifier starting with @@ you cant use in declaring the variable because these are global variable (system defined)

    Eg:
    @@row_count
    @@fetch_status

    Regards
    Vipin Nair
    Tuesday, December 15, 2009 7:35 AM
  • Thanks - this is beginning to make sense now. I also asked someone who uses SQL Server at work & he showed me some DECLARE statements using @ and functions using @@, so this is much clearer now.
    Tuesday, December 15, 2009 9:58 PM
  • you can use it also in creating a table like this.

    declare @myTable as table
    (
    testRow INT identity(1,1)
    )

    Regards,

    Sphinx
    Wednesday, December 16, 2009 3:21 AM