Solved

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

Posted on 2013-12-04
4
513 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

785 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