Nested Temporary Tables

  • Comments posted to this topic are about the item Nested Temporary Tables

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Great question! Thank-you.

    Really appreciate the effort put forward to bring tthe uncertainity of using temp tables of the same name but across purposes.

    I would have liked the query though (instead of the image) so that one can study it across multiple environments.

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

    Follow me on
    Twitter: @sqltwins

  • I think the question is very good as well, but I am not sure if the answer is correct.

    I chose

    Error: "Column name or number of supplied values does not match table definition."

    for the following reason:

    It may not be defined into which table the data is inserted, but the error seems to be raised all the time.

    CREATE TABLE #Tables (tablename sysname, rowcnt int, nullable varchar)

    GO

    CREATE PROCEDURE #Proc

    AS

    CREATE TABLE #t (a int NOT NULL )

    INSERT #t SELECT 1 UNION SELECT 2 -- Insert two rows

    -- Check which tables we inserted rows into

    INSERT #Tables SELECT O.name, P.row_count, (SELECT is_nullable FROM sys.columns WHERE name = 'a' AND object_id = O.object_id) from sys.objects O, sys.dm_db_partition_stats P WHERE O.name LIKE '#t\_%' ESCAPE ('\') AND O.object_id = P.object_id

    GO

    GO

    CREATE TABLE #t (a int NULL)

    GO

    INSERT #t SELECT 1 -- Insert one row

    EXEC #Proc

    GO

    SELECT * FROM #Tables

    DROP TABLE #t

    DROP TABLE #Tables

    DROP PROCEDURE #Proc

    GO

    In the above code example, you will see that most of the time, 1 row is inserted into the table with the nullable column, and two rows into the other one. Occationally you see that the two are exchanged and this is the "not defined" portion.

    If you exchange the two insert statements, the above rules are also switched (most of the time 2 rows are inserted into the nullable collumn and only some times vice versa).

    Now to the error portion. If you add another column (e.g. b sysname) to one of the CREATE TABLE statements, you will get the column list error message. This error is raised all the time, and also when you switch the table to which you add this extra column. To me this is a strong indication that this error is not "undefined".

    Pls let me know your thoughts

    Best Regards,

    Chris Büttner

  • I typed the code into my SSMS as I wasn't sure of the answer, and I definitely got the error complaining about the number of columns. I could be doing something wrong? I have SQL 2008 developer edition.

  • Excellent question!

    I got it wrong on the strength of some tests I ran that gave me the 'Column name or number of supplied values does not match table definition.' error. I failed at scrolling far enough in BOL.

    The important bit to take home is that temporary tables and nested stored procedures is a recipe for confusion. I vaguely remember being bitten by this in the mid 90's.


    Just because you're right doesn't mean everybody else is wrong.

  • Christian Buettner-167247 (12/16/2010)


    This error is raised all the time, and also when you switch the table to which you add this extra column. To me this is a strong indication that this error is not "undefined".

    Pls let me know your thoughts

    I think you're right. The BOL article clearly states:

    However, for modifications to resolve to the table that was created in the nested procedure, the table must have the same structure, with the same column names, as the table created in the calling procedure.

    So, if the tables have the same name but different structure, any inserts, updates, and deletes will fail with an error.

  • The QOtD asks for what is the output of a specific script and this generates this error:

    Insert Error: Column name or number of supplied values does not match table definition.

    and not all possible similar script that COULD generate another kind of error.

    I want back my points.

  • hmm, I get

    Msg 213, Level 16, State 1, Procedure Proc2, Line 4

    Column name or number of supplied values does not match table definition

    SQL 2008

    Win 2008 R2

    -------------------------------------------------------------------------
    Normal chaos will be resumed as soon as possible. :crazy:

  • Can I just point out that the code is not SQL 2000 compatible, despite what it says at the top, because of the use of ROW_NUMBER()?

  • The real error that is displayed is "Incorrect syntax" because there is a non-matching closing parenthesis in the select statement in dbo.Proc1 😛

  • I got 10 rows.I am using sqlserver 2008.

    I think the author answer is correct.It s depends

    Malleswarareddy
    I.T.Analyst
    MCITP(70-451)

  • I don't think there was an "it depends" answer. It seems the question is flawed to the point where you can't say that a single answer is always true, most people who have responded thus far got the error, not a set of records.

  • Nice question and an important point, but I have executed the code a 100 times or more and I always get the error.

    Statistically I'm allowed to say that the correct answer is the error 😀

    edit: pfiew, it took me a long time before I could post on this thread because the site went down. When I went back to the questions I got the SSC maintenance screen. After a few hours I still got the maintenance screen, so I had to manually flush the cache of internet explorer to be able to see the questions again.

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

  • Interesting question, even more interesting explanation. Hmmm. I guess my take away from this question, is don't use temp tables in stored procedures.

  • The question clearly states "What will be the results of the following?". Not what does the Books Online say about nested temp tables using the same name.

    Why is the answer for a differant question?

    Why do you say that this code is compatable on versions of SQL server it is not?

    This is the type of confusing trick question that requires the ability to read the authors mind to resolve.

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

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