Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Nested Temporary Tables Expand / Collapse
Author
Message
Posted Wednesday, December 15, 2010 8:45 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:51 AM
Points: 5,361, Visits: 8,927
Comments posted to this topic are about the item Nested Temporary Tables

Wayne
Microsoft Certified Master: SQL Server 2008
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, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1035615
Posted Thursday, December 16, 2010 12:29 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, September 29, 2014 7:39 AM
Points: 1,426, Visits: 1,841
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
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #1035672
Posted Thursday, December 16, 2010 1:42 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 1:05 AM
Points: 2,842, Visits: 3,875
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
Post #1035685
Posted Thursday, December 16, 2010 1:57 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 10, 2012 7:49 AM
Points: 49, Visits: 10
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.
Post #1035690
Posted Thursday, December 16, 2010 2:06 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 6:36 AM
Points: 3,050, Visits: 938
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.
Post #1035693
Posted Thursday, December 16, 2010 2:16 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
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.
Post #1035698
Posted Thursday, December 16, 2010 2:33 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 7:33 AM
Points: 2,542, Visits: 2,410
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.
Post #1035704
Posted Thursday, December 16, 2010 2:51 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, September 18, 2014 5:45 AM
Points: 568, Visits: 1,603
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.
Post #1035708
Posted Thursday, December 16, 2010 3:26 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 7:18 AM
Points: 1,594, Visits: 5,633
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()?
Post #1035723
Posted Thursday, December 16, 2010 3:36 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 23, 2012 6:47 AM
Points: 15, Visits: 134
The real error that is displayed is "Incorrect syntax" because there is a non-matching closing parenthesis in the select statement in dbo.Proc1
Post #1035726
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse