Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Giving only insert permissions to a table for a new loginRedis and VB.Net »
    comments

    The other day I was doing some testing and then from the same connection I executed a stored procedure only to be greeted with the following message

    Msg 213, Level 16, State 1, Procedure prTestTemp, Line 5
    Column name or number of supplied values does not match table definition.

    I looked at the proc, hasn't changed in months, I decided to run it from a different window and no problem. I took me a couple of minutes to realize what was going on.

    Let's duplicate this here with some code that you can run. Make sure that you run the code all in the same window

    First create this stored procedure, do not close this window after creation

    1. CREATE PROCEDURE prTestTemp
    2. AS
    3.  
    4. CREATE TABLE #temp (id int)
    5. INSERT #temp VALUES(1)
    6.  
    7. SELECT * FROM #temp
    8. GO

    In the same window now create the following temp table

    1. CREATE TABLE #temp (id int, id2 int)
    2. INSERT #temp VALUES(1,2)
    3.  
    4. SELECT * FROM #temp

    Now run the procedure

    1. EXEC prTestTemp

    Here is the error
    Msg 213, Level 16, State 1, Procedure prTestTemp, Line 5
    Column name or number of supplied values does not match table definition.

    Drop the table and we will try again

    1. DROP TABLE #temp

    Run the procedure again

    1. EXEC prTestTemp

    This time there was no error

    Let's do another experiment, create the table again

    1. CREATE TABLE #temp (id int, id2 int)
    2. INSERT #temp VALUES(1,2)
    3.  
    4. SELECT * FROM #temp

    Now, let's try modifying the procedure, change create to alter and run it again

    1. ALTER PROCEDURE prTestTemp
    2. AS
    3.  
    4. CREATE TABLE #temp (id int)
    5. INSERT #temp VALUES(1)
    6.  
    7. SELECT * FROM #temp
    8. GO

    Here is the error
    Msg 213, Level 16, State 1, Procedure prTestTemp, Line 5
    Column name or number of supplied values does not match table definition.

    As you can see, you can't modify the procedure in the same window, copy and paste the code in another window and you won't have a problem.

    The reason you run into this because the temporary table is local to your connection, it is not dropped until you close the connection. If you have a temporary table with the same name inside a proc that you try to execute you will run into this problem. One way to avoid this is by not naming a temporary table the same in every stored procedure that you have, for example #temp

    BTW, doing something like this is no problem

    1. CREATE PROCEDURE prTestTemp2
    2. AS
    3.  
    4. CREATE TABLE #temp (id int, id2 int)
    5. INSERT #temp VALUES(1,2)
    6.  
    7. SELECT * FROM #temp
    8.  
    9. EXEC prTestTemp
    10. GO

    As you can see both procedure have a temporary table named #temp and you get back two resultsets, one has 1 column, the other one has 2 columns

    Just be aware of how this works because you could be scratching your head for hours trying to figure something like this out

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2334 views
    InstapaperVote on HN

    8 comments

    Comment from: Kevin Conan [Member]
    *begin rant* The best way around this is to always drop temp tables in the proc when finished with them. Otherwise you could leave very large tables "stuck" in tempdb therefore bloating tempdb. *end rant*
    03/04/13 @ 10:03
    Comment from: SQLDenis [Member] Email
    SQLDenis Doesn't help with this scenario, you still get the same problem
    03/04/13 @ 10:26
    Comment from: Kevin Conan [Member]
    I ran this and it worked:


    CREATE PROCEDURE prTestTemp

    AS



    CREATE TABLE #temp (id int)

    INSERT #temp VALUES(1)

    SELECT * FROM #temp

    DROP TABLE #temp
    GO


    CREATE TABLE #temp (id int, id2 int)

    INSERT #temp VALUES(1,2)



    SELECT * FROM #temp

    DROP TABLE #temp

    GO

    EXEC prTestTemp

    DROP PROC prTestTemp
    03/04/13 @ 12:17
    Comment from: SQLDenis [Member] Email
    SQLDenis But you dropped the table before running the proc

    CREATE TABLE #temp (id int, id2 int)

    INSERT #temp VALUES(1,2)



    SELECT * FROM #temp

    DROP TABLE #temp

    GO

    EXEC prTestTemp

    Try this

    CREATE TABLE #temp (id int, id2 int)

    INSERT #temp VALUES(1,2)



    SELECT * FROM #temp

    --DROP TABLE #temp

    GO

    EXEC prTestTemp
    03/04/13 @ 12:55
    Comment from: SQLDenis [Member] Email
    SQLDenis Here is the whole thing to run





    CREATE PROCEDURE prTestTemp
    AS
    CREATE TABLE #temp (id int)

    INSERT #temp VALUES(1)
    SELECT * FROM #temp
    DROP TABLE #temp
    GO


    CREATE TABLE #temp (id int, id2 int)
    INSERT #temp VALUES(1,2)
    SELECT * FROM #temp
    --DROP TABLE #temp
    GO

    EXEC prTestTemp

    DROP PROC prTestTemp
    03/04/13 @ 12:59
    Comment from: Kevin Conan [Member]
    Right, my rant is that people should be dropping the table when they are finished with it. In this case you are finished with the temp table before you run the proc so you should drop it first.
    03/04/13 @ 13:15
    Comment from: Kevin Conan [Member]
    I don't mean to detract from what you saying. I could easily see a screnio where you create a temp table, call a proc to pull in more data into a temp table and then process/do something with both temp tables at the same time (which would lead to the issue that you hit on). People should also name their table tables somthing meaningful to help to avoid this issue too (as you stated).
    03/04/13 @ 13:19
    Comment from: SQLDenis [Member] Email
    SQLDenis Yep, the only thing is I had a bunch of code so I didn't even notice that the table was in my query window, it was a bunch of ad-hoc queries, the table was all the way on top
    03/04/13 @ 13:45

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)