December 15, 2010 at 8:45 pm
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
December 16, 2010 at 12:29 am
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
December 16, 2010 at 1:42 am
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
December 16, 2010 at 1:57 am
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.
December 16, 2010 at 2:06 am
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.
December 16, 2010 at 2:16 am
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.
December 16, 2010 at 2:33 am
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.
December 16, 2010 at 2:51 am
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:
December 16, 2010 at 3:26 am
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()?
December 16, 2010 at 3:36 am
The real error that is displayed is "Incorrect syntax" because there is a non-matching closing parenthesis in the select statement in dbo.Proc1 😛
December 16, 2010 at 3:40 am
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)
December 16, 2010 at 3:42 am
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.
December 16, 2010 at 6:17 am
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
December 16, 2010 at 7:37 am
Interesting question, even more interesting explanation. Hmmm. I guess my take away from this question, is don't use temp tables in stored procedures.
December 16, 2010 at 7:43 am
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