SELECT @local_variable

  • Comments posted to this topic are about the item SELECT @local_variable



    See, understand, learn, try, use efficient
    © Dr.Plch

  • If the SELECT statement returns no rows, the variable retains its present value. If expression is a scalar subquery that returns no value, the variable is set to NULL.

    That isn't true.

    Count() return 0

    create table xxx (i int identity, a varchar(3))

    go

    declare @txt varchar(255)set @txt = 'Question'

    select @txt = count(1) from xxx order by 1

    insert xxx values ('of')insert xxx values ('the')insert xxx values ('day')

    select @txt = @txt + ' ' + a from xxx order by i

    select @txt

  • Carlo Romagnano (12/14/2009)


    That isn't true.

    Count() return 0

    This is not true that "That isn't true" 🙂

    "SELECT COUNT" statement from your example returns a zero as a result, so the result contains one row. Thus the condition "the SELECT statement returns no rows" fails and the variable changes its value.

  • Hm, is someone able to explain this?

    Same code as before, but positioned order by (1 instead of i) and "-" as separator for better readability.

    SET NOCOUNT ON

    create table #xxx (i int identity, a varchar(3))

    go

    declare @txt varchar(255)

    set @txt = 'Question' select @txt = isnull (a, '?') from #xxx order by i insert #xxx values ('of')

    insert #xxx values ('the')

    insert #xxx values ('day') select @txt = @txt + '-' + a from #xxx order by 1

    select @txt

    DROP TABLE #xxx

    Result:

    Question-the

    Looks like only the first or last (depending on DESC or ASC sort) row is used in the end result.

    Is that expected behaviour?

    Best Regards,

    Chris Büttner

  • Christian Buettner-167247 (12/14/2009)


    Hm, is someone able to explain this?

    Same code as before, but positioned order by (1 instead of i) and "-" as separator for better readability.

    As said in explanation:

    The second construct is not documented, but works this same way on MS SQL Server 2000, 2005, and 2008.

    I don't know how the undocumented construct works. But there are another questions: What is the first column in your select? Is it OK to order on missing column?



    See, understand, learn, try, use efficient
    © Dr.Plch

  • honza.mf (12/14/2009)


    Christian Buettner-167247 (12/14/2009)


    Hm, is someone able to explain this?

    Same code as before, but positioned order by (1 instead of i) and "-" as separator for better readability.

    As said in explanation:

    The second construct is not documented, but works this same way on MS SQL Server 2000, 2005, and 2008.

    I don't know how the undocumented construct works. But there are another questions: What is the first column in your select? Is it OK to order on missing column?

    Hi Honza,

    I know it's not documented - I just would like to know if someone knows what is going on behind the scenes. I am just curious. My question was not meant to be a challenge for your QotD.

    But with regards to your question

    Is it OK to order on missing column?

    It "might" be OK, since SQL Server is not generating an error.

    But it might as well be not OK (to which I tend more)

    But take the "unsupported" SET statement from your QotD - is it OK to use it as it is? From a SQL standpoint it should not be valid, but obviously it seems to work and you can do some useful stuff with it.

    Now don't ask me for what you could use the positioned order by in my example - actually i just misread your example and exchanged the I with a 1 accidentially. Took me a while to figure out what I had done wrong...

    Best Regards,

    Chris Büttner

  • Christian Buettner-167247 (12/14/2009)


    Same code as before, but positioned order by (1 instead of i)

    select @txt = @txt + '-' + a from #xxx order by 1

    Not the same code. Your code is equal to select @txt = @txt + '-' + a from #xxx order by @txt + '-' + a.

    Looks like SQL Server doesn't use recursive assignment in this case. SQL Server cannot change the value of a variable and, at the same time, sort data using this volatile variable.

    When "order by i" is used, SQL Server can assign values to the variable recursively.

    This is just my guess 🙂

  • I guessed rigtht, but only because I knew it had to begin with 'Question' and there was only one answer that did that. If there'd been an option for 'Question day' then I'd have gone for that and got it wrong 🙂

    Thinking about it, if you do

    select @txt = ' ' + a from xxx order by i

    then the variable ends up with the value 'day'. This is presumably because it applies the assignment to each and every row, so it ends up with the value of the last one. I'd assumed that it was more 'intelligent' than this and just did the assignement once, for the last row. That is what is implied by BOL which says "If the SELECT statement returns more than one value, the variable is assigned the last value that is returned."

    ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/8e1a9387-2c5d-4e51-a1fd-a2a95f026d6f.htm

    But if it actually does it once per row then it makes sense that

    select @txt = @txt + ' ' + a from xxx order by i

    should end up with a concatentation of all the values.

  • Toreador (12/14/2009)


    I guessed rigtht, but only because I knew it had to begin with 'Question' and there was only one answer that did that. If there'd been an option for 'Question day' then I'd have gone for that and got it wrong 🙂

    I wanted to mix two things and not to make it too complicated.

    There was a very similar QotD few weeks ago. I was afraid this one will become much more simple. And it was taken 2 points 😛

    I will think about it, if I will write some other QotD



    See, understand, learn, try, use efficient
    © Dr.Plch

  • Christian Buettner-167247 (12/14/2009)


    Hi Honza,

    I know it's not documented - I just would like to know if someone knows what is going on behind the scenes. I am just curious. My question was not meant to be a challenge for your QotD.

    But with regards to your question

    Is it OK to order on missing column?

    It "might" be OK, since SQL Server is not generating an error.

    But it might as well be not OK (to which I tend more)

    But take the "unsupported" SET statement from your QotD - is it OK to use it as it is? From a SQL standpoint it should not be valid, but obviously it seems to work and you can do some useful stuff with it.

    Now don't ask me for what you could use the positioned order by in my example - actually i just misread your example and exchanged the I with a 1 accidentially. Took me a while to figure out what I had done wrong...

    I don't know what is going on behind the scenes.

    Sometimes I do some type of mental reverse engineering, just trying "How do I programme this".

    This additive assignment to local variables I feel to be an implicit cursor. I suppose the server creates something like a cursor and makes an assignment in it. I haven't compared yet the efficiency of these two variants (single select assignment and cursor). At least it is less coding.



    See, understand, learn, try, use efficient
    © Dr.Plch

  • Christian Buettner-167247 (12/14/2009)


    Hm, is someone able to explain this?

    Same code as before, but positioned order by (1 instead of i) and "-" as separator for better readability.

    SET NOCOUNT ON

    create table #xxx (i int identity, a varchar(3))

    go

    declare @txt varchar(255)

    set @txt = 'Question' select @txt = isnull (a, '?') from #xxx order by i insert #xxx values ('of')

    insert #xxx values ('the')

    insert #xxx values ('day') select @txt = @txt + '-' + a from #xxx order by 1

    select @txt

    DROP TABLE #xxx

    Result:

    Question-the

    Looks like only the first or last (depending on DESC or ASC sort) row is used in the end result.

    Is that expected behaviour?

    Interesting, I see the same results when using the "order by 1" in the variable assignment.

    In SSMS 2008, the query will run but it throws a warning prior to execution.

    "The Order By position number 1 is out of range of the number of items in the select list."

    I believe the results being returned are due to the out of range error.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • I find the variable assignment rather interesting in that it seems to occur at a curious point. To see what happened when there is a null value, I tried:

    declare @txt varchar(255)

    set @txt = 'Question'

    insert xxx values ('')

    select @txt = isnull (a, '?') from xxx order by i

    and got a null string and not "?" as I was expecting.

    Does you know why? It seems to me that the variable assignment occurs at a rather odd place in the execution of the statement.

  • Richard Gibbins (12/14/2009)


    To see what happened when there is a null value, I tried:

    declare @txt varchar(255)

    set @txt = 'Question'

    insert xxx values ('')

    select @txt = isnull (a, '?') from xxx order by i

    and got a null string and not "?" as I was expecting.

    In SQL Server, an empty string and a NULL value are not the same.

    It can be shown by this:

    create table #xxx (i int identity, a varchar(3))

    declare @txt varchar(255)

    set @txt = 'Question'

    insert #xxx values ('')

    select @txt = isnull (a, '?') from #xxx order by i

    select @txt -- the result is '' (empty string)

    insert #xxx values (null)

    select @txt = isnull (a, '?') from #xxx order by i

    select @txt -- the result is '?'

    select @txt = a from #xxx order by i

    select @txt -- the result is NULL

    drop table #xxx

    and this:

    declare @var varchar(255)

    set @var = ''

    if @var is null select 'NULL' else select 'NOT NULL' -- the result is 'NOT NULL'

    set @var = null

    if @var is null select 'NULL' else select 'NOT NULL' -- the result is 'NULL'

    As far as I know, NULLs and empty strings are equal in Oracle. But in SQL Server they are not.

  • Ah! Good catch on Oracle vs Sql Server; I didn't realize it was that obvious:-).

    Thanks for the explanation; it makes more sense than what I was imagining.

    Richard

  • vk-kirov (12/14/2009)


    In SQL Server, an empty string and a NULL value are not the same.

    As far as I know, NULLs and empty strings are equal in Oracle. But in SQL Server they are not.

    To distinguish NULLs and empty strings or to find padding spaces I use

    select '«' + @txt + '»' The difference is visible at the first look. You can use some other type of quotes or brackets if you like.

    Sorry to oraclists, I am used to MS SQL Server.



    See, understand, learn, try, use efficient
    © Dr.Plch

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

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