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
Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

856 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