Declare @local variable

  • steve.jacobs

    SSCommitted

    Points: 1830

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

  • Mr. Kapsicum

    SSCertifiable

    Points: 6128

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

  • twin.devil

    SSC-Insane

    Points: 22208

    i almost clicked on "YES". 😛

    Thanks for sharing

  • This was removed by the editor as SPAM

  • philip.cullingworth

    SSCrazy

    Points: 2150

    Mr. Kapsicum (12/4/2013)


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

    And those with current experience of 2005:-)

  • paul s-306273

    SSChampion

    Points: 10602

    Easy (maybe too easy?).

  • SQLDoubleG

    Hall of Fame

    Points: 3090

    Thanks for the easy one!

  • Arthur Olcot

    SSCertifiable

    Points: 6008

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

  • steve.jacobs

    SSCommitted

    Points: 1830

    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:

  • OCTom

    SSChampion

    Points: 11755

    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

  • Carlo Romagnano

    SSC-Insane

    Points: 21830

    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 varchar= @a

  • Ken Wymore

    SSCoach

    Points: 16429

    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!

  • steve.jacobs

    SSCommitted

    Points: 1830

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

  • Jason Wolfkill

    SSCrazy Eights

    Points: 9772

    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

  • Dave62

    SSCertifiable

    Points: 6463

    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 25 total)

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