T-SQL

  • Comments posted to this topic are about the item T-SQL

  • Nice and easy...

  • I tried on Sql 2005,

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 137, Level 15, State 2, Line 4

    Must declare the scalar variable "@str1".

    Am I testing it in a wrong way?

  • the answer is only correct when using SqlServer 2012 and above. Previous versions prompt an error.

  • l.vignando (12/19/2014)


    I tried on Sql 2005,

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 137, Level 15, State 2, Line 4

    Must declare the scalar variable "@str1".

    Am I testing it in a wrong way?

    if you try this on 2005 you will need to split the variable assignment, try this:

    @str1 VARCHAR(5)

    ,@str2 VARCHAR(10) ;

    select @str1 = NULL, @str2 = 'SQL Strings';

  • I use SQL Server 2008 R2 and it works fine..

  • SALIM ALI (12/19/2014)


    the answer is only correct when using SqlServer 2012 and above. Previous versions prompt an error.

    2008 is quite enough.

  • erwin.oosterhoorn (12/19/2014)


    l.vignando (12/19/2014)


    I tried on Sql 2005,

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 137, Level 15, State 2, Line 4

    Must declare the scalar variable "@str1".

    Am I testing it in a wrong way?

    if you try this on 2005 you will need to split the variable assignment, try this:

    @str1 VARCHAR(5)

    ,@str2 VARCHAR(10) ;

    select @str1 = NULL, @str2 = 'SQL Strings';

    In-line assignment was introduced in version SQL 2008

    DECLARE

    {

    { @local_variable [AS] data_type | [ = value ] }

    | { @cursor_variable_name CURSOR }

    } [,...n]

    Prior to SQL 2008, declaration and assignment happens in two different lines.

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • While yes, initial assignment was introduced in SQL 2008, I think the point of the question was what it prints out and the data types involved. I thought it was a good question that raised a good point.

  • This was removed by the editor as SPAM

  • Interesting Question, thanks!

  • Nice simple question, thanks.

  • Thanks for the question. Good way to start Friday.



    Everything is awesome!

  • Nice easy one for a Friday. Thanks for submitting the question!

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • I overthought this one but still got it right. Nice question.

    Not all gray hairs are Dinosaurs!

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

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