Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-04
4
Medium Priority
?
559 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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 70

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

704 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