Solved

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

Posted on 2013-12-04
4
504 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:
ScottPletcher 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:ScottPletcher
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help on Setting up an identical test database 17 53
Query Missing Money orders... 6 73
Report Builder 9 31
Azure SQL DB? 3 22
In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now