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

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.

Why?

Kevin
LVL 81
zorvek (Kevin Jones)ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
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

see: http://msdn.microsoft.com/en-us/library/ms187400.aspx
and there you will see that exactly 5 columns are produced as the result set.

Solution, define the 5 columns in your table variable.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zorvek (Kevin Jones)ConsultantAuthor Commented:
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
Link: http://www.sommarskog.se/share_data.html

Kevin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.