Unable to Select Single Field from SP Result Set into Declared Table

Posted on 2014-08-30
Last Modified: 2014-08-30
This works:

            DECLARE @DBMailQueueStatus AS TABLE (
                  queue_type varchar(20),
                  [length] int,
                  [state] varchar(20),
                  last_empty_rowset_time datetime,
                  last_activated_time datetime
            INSERT INTO @DBMailQueueStatus EXEC msdb.dbo.sysmail_help_queue_sp @queue_type = 'mail'

This does not:

            DECLARE @DBMailQueueStatus AS TABLE (
                  [state] varchar(20)
            INSERT INTO @DBMailQueueStatus([state]) EXEC msdb.dbo.sysmail_help_queue_sp @queue_type = 'mail'

The error is:

Msg 213, Level 16, State 7, Procedure sysmail_help_queue_sp, Line 57
Column name or number of supplied values does not match table definition.


Question by:zorvek (Kevin Jones)
    LVL 47

    Accepted Solution

    The stored procedure is returning more columns than you have in the table variable.

    Msg 213, Level 16, State 7, Procedure sysmail_help_queue_sp, Line 57
    Column name or number of supplied values does not match table definition.

    It works when table variable has 5 columns
    It fails when table variable has 1 column

    Hence, the stored procedure's output has more than one column but less than 6 columns

    and there you will see that exactly 5 columns are produced as the result set.

    Solution, define the 5 columns in your table variable.
    LVL 81

    Author Comment

    by:zorvek (Kevin Jones)
    True that. This page agrees with you with the statement:

    "The result set from the called procedure must match the column list in the INSERT statement exactly. The procedure may produce multiple result sets, and that's alright as long as all of them match the INSERT statement."

    Title: How to Share Data between Stored Procedures


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
    Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now