What is the result of the following query

  • Comments posted to this topic are about the item What is the result of the following query

  • This is a very good back to basics question, thank you.

    The behaviour of the exec in this case is identical to the one of the stored proc where the temp table created inside is descoped once the proc bails out. In other words, if create temp table part were moved out then the temp table would be visible to the exec batch and after, i.e.

    if object_id('tempdb.dbo.#tmp') is not null drop table #tmp;

    create table #tmp (id int);

    declare @sql varchar(2000);

    set @sql = 'insert into #tmp(id) values (1);';

    exec (@sql);

    select * from #tmp;

    results in

    id

    -----------

    1

    Oleg

  • Good question but the answers are a bit... unfair.

    The 2 answers:

    Throw an error: Invalid object name '#tmp'.

    Throw an error: Cannot find table #tmp inside TempDB.

    Have the same effect (no #tmp table). So you would also have to know the wording of the error SQL would throw in order to get it right... without running it.

    /T

  • Very good, basic question. Clearly signifies the importance of scoping things right, which most tend to forget when piling on tons of code.

    Thank-you!

    Thanks & Regards,
    Nakul Vachhrajani.
    http://nakulvachhrajani.com

    Follow me on
    Twitter: @sqltwins

  • Nice question, thanks.

    (but it surprises me that apparently 78% -for the moment- know the exact error message!)

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • da-zero (12/10/2010)


    Nice question, thanks.

    (but it surprises me that apparently 78% -for the moment- know the exact error message!)

    Could have done what i did... ran the code. Now i knew that it wouldnt find the #tmp table but didnt know the exact error message (like i care about the exact wording of an errormessage). Cheating... maybe maybe not... it depends 😛

    Like i wrote a bit up... the second error message about not being able to find #tmp shouldnt have been there.

    /T

  • da-zero (12/10/2010)


    (but it surprises me that apparently 78% -for the moment- know the exact error message!)

    Well, I don't know about others, but for me this is one of the more common errors I see when developing. Or when copying and pasting code snippets from internet support forums. I would not be able to reproduce it out of the blue, but I did recognise the "invalid object name" error, and I have not ever seen a message that even resembles the "cannot find ... inside TempDB" one. (plus, that one is extremely unlikely, as the database for temporary object is called tempdb, not TempDB - but I guess only people who are in the habit of developing on a case sensitive instance would know that).


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • I got it right but I had to think about this one.

    I thought the error was going to be caused by not having a space between the two commands.

    Set @strSql = @strSql + 'Create table #tmp (id int)'

    Set @strSql = @strSql + 'Insert into #tmp(id) values (1)'

    This should produce the same as (Notice no spacing between two commands):

    Set @strSQL = 'Create table #tmp (id int)Insert into #tmp(id) values (1)'

    I knew this had to cause some error because there's no space or semicolon between the create and insert statements but didn't know the exact error that would be produced. I've never seen any of the other specific errors so I guessed correctly but for the wrong reason.

  • cengland0 (12/10/2010)


    This should produce the same as (Notice no spacing between two commands):

    Set @strSQL = 'Create table #tmp (id int)Insert into #tmp(id) values (1)'

    I knew this had to cause some error

    Except it doesn't.

    If you copy/paste the above in an SSMS query window and hit execute, you get "Command(s) completed successfully."


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (12/10/2010)


    Except it doesn't.

    If you copy/paste the above in an SSMS query window and hit execute, you get "Command(s) completed successfully."

    Exactly. I try to answer the questions without executing the code so I thought the missing space would cause an error.

    Remember, there are so many trick QOTD's so I'm always looking for the trick. I thought I found the trick and it was that a space was missing. The lack of the space actually had nothing to do with the error but I thought it did so I selected the error message I thought would have been generated and got it right but for the wrong reason.

  • Got it right for the wrong reason, learned something. Thanks.

  • Oleg Netchaev (12/9/2010)


    ... In other words, if create temp table part were moved out then the temp table would be visible to the exec batch and after, i.e.

    if object_id('tempdb.dbo.#tmp') is not null drop table #tmp;

    create table #tmp (id int);

    declare @sql varchar(2000);

    set @sql = 'insert into #tmp(id) values (1);';

    exec (@sql);

    select * from #tmp;

    results in

    id

    -----------

    1

    Oleg

    Another way to get it to work is to put it all on the inside of the dynamic SQL like this:

    Declare @strSql varchar(2000);

    Set @strSql = '';

    Set @strSql = @strSql + 'Create table #tmp (id int);';

    Set @strSql = @strSql + 'Insert into #tmp(id) values (1);';

    Set @strSql = @strSql + 'Select * From #tmp;';

    Exec (@strSql);

  • cengland0 (12/10/2010)


    I thought I found the trick and it was that a space was missing. The lack of the space actually had nothing to do with the error but I thought it did so I selected the error message I thought would have been generated and got it right but for the wrong reason.

    I'll admit that I expected the missing space to be the issue too, when I saw the question in the daily mail. But when I went to the sit, saw the answer options, and missed any option that I could relate to this missing space issue, I realized that apparently either the missing space is not a problem, or the author accidentally forgot the space and didn't realize. So I answered the question, then copied and executed the code to check that indeed, no space is required here.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (12/10/2010)


    da-zero (12/10/2010)


    (but it surprises me that apparently 78% -for the moment- know the exact error message!)

    Well, I don't know about others, but for me this is one of the more common errors I see when developing. Or when copying and pasting code snippets from internet support forums.

    Hugo makes a good point. I am sure more than 78% of us have submitted or executed code from this very web site that has returned this error.

    <shhhhhh>

    But I still always check my work by running the code.

  • Since the error is due to the #tmp table no longer being in scope once the EXEC() is done, one could fix this to work by making the table global and adding an explicit DROP after its last use. This leaves the table available to the SELECT.

    Declare @strSql varchar(2000)

    Set @strSql = ''

    Set @strSql = @strSql + 'Create table ##tmp (id int)' -- Now "##tmp', not "#tmp'

    Set @strSql = @strSql + 'Insert into ##tmp(id) values (1)'

    Exec (@strSql)

    Select * from ##tmp

    drop table ##tmp

    Of course, in practice, one must be careful in naming global temp tables to avoid possible collisions with unrelated processes.

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

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