Solved

nested stored procedure(sp2) within a stored procedure1(sp1) to use the output of sp2 as an input to sp2

Posted on 2010-11-30
3
423 Views
Last Modified: 2012-05-10
Hi experts,
I have to create a stored procedure which has a nested stored procedure within, the structure should look like:

CREATE PROCEDURE SP1
      @params            int,
      
AS
 
      
      BEGIN
            SELECT col1, col2 ... coln
            FROM yourTable
            WHERE somecolumn = @params;
      END;
 
        
 
CREATE PROCEDURE SP2
      select c1,c2 from table

The output column c2 of of SP2 should be the input @params of SP1,
should I write a cursor to achieve this or should it be a nested procedure? please help me with the sql for this.

The @params will get multiple values and which are passed through the SP1 to achieve results, not sure if I am clear, please let me know of any questions.
Thanks!
0
Comment
Question by:shyamaladevib
[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
3 Comments
 
LVL 15

Accepted Solution

by:
AmmarR earned 250 total points
ID: 34246683
hi shyamaladevib

whats your goal

if all you want is the output of SP1

you can do the following

SELECT col1, col2 ... coln
            FROM yourTable
            WHERE somecolumn in (select c2 from table)

---

i mean this script above will get you all the records from yourTable that Somecolumn is in c2 of table.

or if you want to can convert your SP1 into a function and call it in a select statment of sp2

like

select c1,c2, dbo.SP1(c2) from table

this will send c2 to the function in every record and but will retrieve a table from the SP1.

i guess if you define your output clearly, i will be able to help you better.

regards
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 250 total points
ID: 34278188
Hi

-- in spOutter
-- create a temp table
create table #TempTable(
  col1 int
  , col2 varchar( 20 )

insert #TempTable
exec spInner @Parameter1 = SomeValue
;

Notes - have to get the columns and types to match output from spInner. Obviousyly can't have multiple outputs from spInner.

In SQL2008 think about using TableValued functions and TableVariables instead of this old way.

For instance,

select *
from function_was_spInner( SomeValue )
;

Regards
  David
0
 

Author Closing Comment

by:shyamaladevib
ID: 34293007
Thanks!
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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