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?

>
EXPERT RESPONSE
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.


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 and SQL Server Tutorial and Reference Guide
SQL Server stored procedures tutorial: Write, tune and get examples
Check SQL Server database and log file size with this stored procedure
SQL Server source code analysis and management adds database security
Configure SQL Server Service Broker for sending stored procedure data
Find size of SQL Server tables and other objects with stored procedure
Track changes to SQL Server 2000 and 2005 with one simple utility
Troubleshoot SQL Server 2005 temporary table performance problems
Use SQL Profiler to find long running stored procedures and commands
Stored procedure to monitor long-running jobs in SQL Server 2000

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



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

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

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




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