SQL Server Performance

Declare @

Discussion in 'General Developer Questions' started by Madhivanan, May 23, 2007.

  1. Madhivanan Moderator

    I wonder why I didnt get any error like Incorrect syntax near @


    declare @ int
    set @=1
    select @ as variable_with_no_name

    Is this bug or Special Feature?

    Madhivanan

    Failing to plan is Planning to fail
  2. satya Moderator

    I get '1' as results on my SQL 2000 instance, which I believe what you are expecting.

    Satya SKJ
    Microsoft SQL Server MVP
    Writer, Contributing Editor & Moderator
    http://www.SQL-Server-Performance.Com
    This posting is provided AS IS with no rights for the sake of knowledge sharing. Knowledge is of two kinds. We know a subject ourselves or we know where we can find information on it.
  3. Adriaan New Member

    The "unnamed" variable works against SQL 7.0 as well. Seems like they never heard of variable naming conventions.[<img src='/community/emoticons/emotion-4.gif' alt=':p' />]<br /><br />Is this the complete script? "@" is probably the last thing that makes sense in your script, which is why it is reported by the "Incorrect syntax near ..." error message.
  4. Madhivanan Moderator

    quote:Originally posted by satya

    I get '1' as results on my SQL 2000 instance, which I believe what you are expecting.

    Satya SKJ
    Microsoft SQL Server MVP
    Writer, Contributing Editor & Moderator
    http://www.SQL-Server-Performance.Com
    This posting is provided AS IS with no rights for the sake of knowledge sharing. Knowledge is of two kinds. We know a subject ourselves or we know where we can find information on it.
    I wonder why sql server does throw error for not having variable name

    Madhivanan

    Failing to plan is Planning to fail
  5. Madhivanan Moderator

    <blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by Adriaan</i><br /><br />The "unnamed" variable works against SQL 7.0 as well. Seems like they never heard of variable naming conventions.[<img src='/community/emoticons/emotion-4.gif' alt=':p' />]<br /><br />Is this the complete script? "@" is probably the last thing that makes sense in your script, which is why it is reported by the "Incorrect syntax near ..." error message.<br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote"><br />This is complete sample script<br />But I expect the error <b>Incorrect syntax near @</b> [<img src='/community/emoticons/emotion-1.gif' alt=':)' />]<br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
  6. satya Moderator

    May be service pack differences!

    quote:Originally posted by Madhivanan


    quote:Originally posted by satya

    I get '1' as results on my SQL 2000 instance, which I believe what you are expecting.

    Satya SKJ
    Microsoft SQL Server MVP
    Writer, Contributing Editor & Moderator
    http://www.SQL-Server-Performance.Com
    This posting is provided AS IS with no rights for the sake of knowledge sharing. Knowledge is of two kinds. We know a subject ourselves or we know where we can find information on it.
    I wonder why sql server does throw error for not having variable name

    Madhivanan

    Failing to plan is Planning to fail

    Satya SKJ
    Microsoft SQL Server MVP
    Writer, Contributing Editor & Moderator
    http://www.SQL-Server-Performance.Com
    This posting is provided AS IS with no rights for the sake of knowledge sharing. Knowledge is of two kinds. We know a subject ourselves or we know where we can find information on it.
  7. Adriaan New Member

    Ah - Mahdivanan is wondering why he does NOT get an error message!

    DECLARE @TEST INT
    SELECT @

    Error:
    Must declare the variable '@'.

    So @ is perfectly acceptable as a variable name. It shouldn't be, if you ask me, but that's just the way it is.
  8. FrankKalis Moderator

    When you read _very_ closely through BOL you come to the topic about "Rules for Regular Identifiers" to which variable names ALWAYS MUST comply:

    quote:
    Both regular and delimited identifiers must contain from 1 through 128 characters. For local temporary tables, the identifier can have a maximum of 116 characters.
    ...
    The first character must be one of the following:

    A letter as defined by the Unicode Standard 3.2. The Unicode definition of letters includes Latin characters from a through z, from A through Z, and also letter characters from other languages.

    The underscore (_), at sign (@), or number sign (#).

    Certain symbols at the beginning of an identifier have special meaning in SQL Server. A regular identifier that starts with the at sign always denotes a local variable or parameter and cannot be used as the name of any other type of object. An identifier that starts with a number sign denotes a temporary table or procedure. An identifier that starts with double number signs (##) denotes a global temporary object. Although the number sign or double number sign characters can be used to begin the names of other types of objects, we do not recommend this practice.

    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 @@.

    Subsequent characters can include the following:

    Letters as defined in the Unicode Standard 3.2.

    Decimal numbers from either Basic Latin or other national scripts.

    The at sign, dollar sign ($), number sign, or underscore.

    The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words.

    Embedded spaces or special characters are not allowed.

    Supplementary characters are not allowed.
    Bold markings are by me. Pay special attention to the words "must" and "can". In some weird sophisticated understanding a variable named @ complies to all these rules.

    --
    Frank Kalis
    Moderator
    Microsoft SQL Server MVP
    Webmaster:http://www.insidesql.de
  9. Adriaan New Member

    Not really sophisticated:

    A: "A regular identifier that starts with the at sign always denotes a local variable or parameter"

    B: "identifiers must contain from 1 through 128 characters"

    A+B: You can have a local variable or parameter called @, no problem.
  10. FrankKalis Moderator

    q.e.d <br /><br />...yet strange, and therefore "by design". [<img src='/community/emoticons/emotion-1.gif' alt=':)' />]<br /><br /><br /><br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>
  11. Madhivanan Moderator

    &lt;&lt;<br />B: "identifiers must contain from 1 through 128 characters"<br />&gt;&gt;<br /><br />I thought apart from @, "identifiers must contain from 1 through 128 characters" [<img src='/community/emoticons/emotion-1.gif' alt=':)' />]<br /><br /><br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
  12. FrankKalis Moderator

    Well, apparently not. [<img src='/community/emoticons/emotion-1.gif' alt=':)' />]<br /><br />--<br />Frank Kalis<br />Moderator<br />Microsoft SQL Server MVP<br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>
  13. vindicator New Member

    Can't you just imagine this discussion in the Quality Assurance group at MS?<br /><br />[<img src='/community/emoticons/emotion-5.gif' alt=';)' />]
  14. FrankKalis Moderator

    Better not. [<img src='/community/emoticons/emotion-2.gif' alt=':D' />]<br /><br />--<br />Frank Kalis<br />Microsoft SQL Server MVP<br />Contributing Editor, Writer & Forum Moderator<a target="_blank" href=http://www.sql-server-performance.com>http://www.sql-server-performance.com</a><br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>
  15. Madhivanan Moderator

    Why not? [<img src='/community/emoticons/emotion-1.gif' alt=':)' />]<br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
  16. FrankKalis Moderator

    I guess one would have to be a lawyer to find some joy in such discussions. [<img src='/community/emoticons/emotion-1.gif' alt=':)' />]<br /><br />--<br />Frank Kalis<br />Microsoft SQL Server MVP<br />Contributing Editor, Writer & Forum Moderator<a target="_blank" href=http://www.sql-server-performance.com>http://www.sql-server-performance.com</a><br />Webmaster:<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a>

Share This Page