Home > Ask the SQL Server Experts > Andrew Novick - Development Questions & Answers > How to use results from one stored procedure in another
Ask The SQL Server Expert: Questions & Answers
EMAIL THIS

How to use results from one stored procedure in another

Andrew Novick EXPERT RESPONSE FROM: Andrew Novick

Pose a Question
Other SQL Server Categories
Meet all SQL Server Experts
Become an Expert for this site


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 19 May 2005
I've written a stored procedure for use in a Crystal Report. It works great. I would also like to use the results from this stored procedure in another stored procedure. How do I do this?


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
Andrew Novick - Development
Nullable fields in a table
Connecting Visual Basic to SQL Server
Stored procedures in Crystal Reports
Storing an image file in SQL Server
Storing large amounts of data as ntext data type
Stored procedures looking at different databases
Code trigger that captures login and system date
Stored procedures and remote procedure calls
Using newid in SQL Server to insert records in parent-child tables
How to display the XML result of 'Select For XML Explicit'

SQL Server Stored Procedures
SQL Server Mailbag: CALs, witnesses and unwanted changes
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Top tips and tricks for SQL Server database development
Top 10 SQL Server development tips of 2008
SQL Server trigger vs. stored procedure to receive data notification
SQL Server errors, failures and other problems fixed from the trenches
SQL Server and data manipulation in T-SQL
How to use SQL Server 2008 hierarchyid data type
SQL Server stored procedures tutorial: Write, tune and get examples
Check SQL Server database and log file size with this stored procedure

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
library  (SearchSQLServer.com)
trigger  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


So long as the stored procedure produces only a single result, the technique for using the output of one stored procedure in another is pretty straightforward. The technique is to use a temporary table to hold the results of the stored procedure and an INSERT EXEC statement to execute the sproc and save the results. Once the results are in the temporary table they can be used like any other table data.

Here's an example procedure that we might like to reuse:

CREATE PROC  usp_Demo_AllAuthors as

    select * from pubs..authors
GO

Now here's a stored procedure that uses the results of usp_Demo_AllAuthors:

CREATE proc usp_Demo_SPUser as 

    CREATE TABLE #Authors (
     au_id varchar(11) NOT NULL PRIMARY KEY CLUSTERED,
     au_lname varchar (40) NOT NULL ,
     au_fname varchar (20) NOT NULL ,
     phone char (12) NOT NULL,
     address varchar (40) NULL ,
     city varchar (20) NULL ,
     state char (2) NULL ,
     zip char (5) NULL ,
     contract bit NOT NULL
    )
    
    -- Execute usp_Demo_AllAuthors storing the
    -- results in #Authors
    insert into #Authors
     exec usp_Demo_AllAuthors
    
    -- Here we use the #Authors table.  This example only
    -- only selects from the temp table but you could do much
    -- more such as use a cursor on the table or join with 
    -- other data.
    SELECT au_fName + ' ' + au_lname as [name]
         , address+', '+city+', '+state+' '+zip [Addr] 
        from #Authors
    
    DROP TABLE #Authors
GO


Do you have comments on this Ask the Expert Q&A? Let us know.




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



SQL Solutions - SQL Database Design
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts