Solved

Pass the Result Set of One Stored Procedure into a Second Stored Procedure as an Input Parameter

Posted on 2013-12-04
4
545 Views
Last Modified: 2013-12-07
As the title says, I need to pass a result set from a stored procedure into another as an input parameter.  Here's my situation:

I have a VERY LONG stored procedure that in essence is the heart and soul of my application.  It handles one of the most important features of the app.  But yeah, it's very long.  However it can probably be broken down into two main parts:  the part that selects a bunch of data and the part that inserts said data into a bunch of different places.

So I would like to break it apart into 2 stored procedures just to make it a little more palatable for the junior developers who also need to work on it.  

The result set amounts to 3 columns of data.  I've thought about using a more permanent temp table (I believe @@Temps are permanent until explicitly disposed) in which case I wouldn't even have to pass it as an input parameter I would simply need to reference it.

Anyway, I'm open to suggestions.
0
Comment
Question by:cat4larry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39697024
Any temp table created in a "parent"/calling proc is available to all called procs.  So, if proc1 creates a temp table, then EXECs proc2, proc2 can use proc1's temp tables.

You could also use a non-temp table and add a key to group/batch rows for a subsequent proc to process.  For example:

CREATE TABLE dbo.batch_numbers ( batch_number int IDENTITY(1, 1) NOT NULL )
CREATE TABLE dbo.proc_data ( batch_number int, proc_data_col_1 varchar(30), proc_data_col_2 datetime, .... )


CREATE PROCEDURE dbo.proc1
    @...
AS
SET NOCOUNT ON
DECLARE @batch_number int
INSERT INTO dbo.batch_numbers DEFAULT VALUES
SET @batch_number = SCOPE_IDENTITY()
...rest of big proc, part 1 that loads data into the dbo.proc_data table
EXEC dbo.proc2 @batch_number
GO


CREATE PROCEDURE dbo.proc2
    @batch_number int,
    @...
AS
SET NOCOUNT ON
--process all data in dbo.proc_data table with batch_number = @batch_number
0
 

Author Comment

by:cat4larry
ID: 39697220
For curiosity sake though, how would one pass the result set from one proc into another proc.  Could I use some sort of table variable?

DaveKid
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39700582
No, table variables don't survive outside their immediate scope.

You really should use either a temporary table or a permanent table with a key to identify the row(s) for the proc called to process.
0
 

Author Comment

by:cat4larry
ID: 39703524
K, Thanks
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

626 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