Question

I have an existing stored procedure that essentially returns two tables, the first table being just one integer in an unnamed column, and the second table has rows of named columns.

I need to build a new stored procedure that executes the existing stored procedure and uses the data from the second table returned by the existing stored procedure, and ignores the first table/integer.

The following is my attempt at it, but it returns the error, "Column name or number of supplied values does not match table definition" because of that first table/integer.

CREATE PROCEDURE [dbo].[New_StoredProcedure]
AS
BEGIN
CREATE TABLE #temptable(X VARCHAR(20), Y INT, Z VARCHAR(100))
INSERT INTO #temptable(X, Y, Z)
EXEC [Existing_StoredProcedure]
    @a = '1',
    @b = '2',
    @c = '3'

SELECT * FROM #temptable
DROP TABLE #temptable
END

Is there a way for to write this that ignores the first table/integer?

Was it helpful?

Solution

Another way you can control the returned result set will be adding an additional parameter which will decied how many and which result sets are returned when a stored procedure is called.

For example in your [Existing_StoredProcedure] you can do something like .....

ALTER PROCEDURE [Existing_StoredProcedure]
@a    VARCHAR(10),
@b    VARCHAR(10),
@c    VARCHAR(10),
@ResultSet INT = NULL    --<-- Indicator Param
AS 
BEGIN
  SET NOCOUNT ON;

  -- Common code that executes regardless of what result set is required


  -- Now return the result set based on value passed to @ResultSet       
  IF (@ResultSet = 1 OR @ResultSet IS NULL)
    BEGIN
       -- Code for 1st result set  
    END

  IF (@ResultSet = 2 OR @ResultSet IS NULL)
    BEGIN
       -- Code for 2nd result set  
    END

END 

Now if you dont pass any value to @ResultSet param both result sets will be returned but if you pass 1 or 2 , only relative result set will be returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top