the meaning of @ sign in SQL statement

Posted on 2006-05-25
Last Modified: 2010-04-17
I have seen in some SQL statements they use the @sign in front of variables, I don't know what this means.

example :
Update vendors
set vendorname=@vendorename where vendorID=@@identity

in the statement above you can notice the usage of the @ sign befor a variable, and @@ before the identity word.

any explanations?

Question by:jskfan
    LVL 7

    Assisted Solution

    Its simply the syntax for referencing or declaring a variable in a stored procedure or SQL statement, you can just treat them as variables.

    Hope this helps.
    LVL 7

    Expert Comment

    and there is heaps more here


    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
    LVL 2

    Assisted Solution


    (@@) means internal to SQL Server; there are many of those ready server functions like:

    (@) on the other hand helps in 2-scenarios:
    First: Variables Assignment; for example in Stored Procedures OR User-Defined-Functions you can do the following:

    DECLARE @x int
    SET @x = 5


    DECLARE @mName varchar(50)
    SET @mName = 'Bill Gates'

    DECLARE @mAge int
    SET @mAge = (SELECT Age FROM Master WHERE City='London')

    Second: as you said it; in a Procedure or Function variables; for example:

    @mAge int,
    @mName varchar(100)


    CREATE FUNCTION Calculate_Area
    RETURNS Decimal
    @x int,
    @y int

    hope this help
    best regards;

    Author Comment

    so it's just a convention.

    if I declare a variable:

    DECLARE x int
    SET x = 5

    would it throw an error?

    LVL 5

    Assisted Solution

    ..the idea for @ before the :

    sql consider this variable as a parameter.. so you can pass from your code what ever you want to this parameter..

    if you use this statement

    Update vendors
    set vendorname=@vendorename

    you can pass a value in your code to the parameter the query will be complete..
    this query will not be executed unless provided with @vendorname parameter

    LVL 2

    Accepted Solution


    DECLARE x int
    SET x =5

    YES; will throw an error as SQL will think this is a CURSOR rather that a local variable.

    hope that helps
    best regards

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    sum13 challenge 24 68
    abstract class with all non abstract mentods 6 57
    sumHeights2  challenge 7 50
    countPairs challenge 7 34
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now