• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

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
0
zorvek (Kevin Jones)
Asked:
zorvek (Kevin Jones)
1 Solution
 
PortletPaulCommented:
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now