Declare @local variable

  • Comments posted to this topic are about the item Declare @local variable

  • easy one for the guys who has past experience in 2005 😀

  • i almost clicked on "YES". 😛

    Thanks for sharing

  • This was removed by the editor as SPAM

  • Mr. Kapsicum (12/4/2013)


    easy one for the guys who has past experience in 2005 😀

    And those with current experience of 2005:-)

  • Easy (maybe too easy?).

  • Thanks for the easy one!

  • Love the "maybe" option! made me think twice before answering 🙂

  • paul s-306273 (12/5/2013)


    Easy (maybe too easy?).

    Well I thought I would give an easy one to "make up" for the "Order My Data" question I submitted. But you would be surprised how many developers get this one wrong. :w00t:

  • The first SQL server version I worked with was 2000. I got into the habit of declaring and setting in two statements and I still do that today. I forgot you can declare and set in one statement. :crazy:

    Tom

  • Starting from sqlserver2008, you can declare and set a variable in one statement,

    but you can NOT initialize a second variable with another inside the same declaration.

    declare @a varchar= 'A'

    ,@b varchar= @a

    You should have two DECLARE

    declare @a varchar= 'A'

    declare @b-2 varchar= @a

  • I had to do some research on this one because we jumped from 2000 to 2008. Like many others, I have always set my variables in separate SET statements anyway. It wasn't until I saw someone else's code on a 2008 instance a couple years ago that I saw a value set in the declaration. Thanks for the question Steve!

  • Carlo Romagnano (12/5/2013)


    Starting from sqlserver2008, you can declare and set a variable in one statement,

    but you can NOT initialize a second variable with another inside the same declaration.

    declare @a varchar= 'A'

    ,@b varchar= @a

    You should have two DECLARE

    declare @a varchar= 'A'

    declare @b-2 varchar= @a

    Absolutely true Carlo. Simple reason, query "parser"\optimizer has no idea what @a is. So you are setting it before declaring it (backwards). This is seen in many (if not all) programming languages.

    Thanks

  • Ahh, this brought back bad memories of all the extra typing required to declare and set variables in separate statements. I still say a silent "hallelujah" every time I declare and set a variable in the same statement.

    Jason Wolfkill

  • I got it right but only by guessing if the question was to be answered or the description.

    The problem is that, as written, the question and the description have 2 different answers.

    In SQL Server 2005, is the code segment (a local variable in a stored procedure) below allowed?

    The question "In SQL Server 2005, is the code segment below allowed?" has one answer.

    The description "(a local variable in a stored procedure)" has a different answer.

    In order for the description to match the question it would have to read "(a local variable, declared and set in one line, in a stored procedure)".

    I don't see too many complaints though so maybe I'm the only one who thought there could be a trick in the wording. 😎

    Thanks for the QotD!

Viewing 15 posts - 1 through 15 (of 24 total)

You must be logged in to reply to this topic. Login to reply