[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

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

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
shyamaladevib
Asked:
shyamaladevib
2 Solutions
 
AmmarRCommented:
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
 
David ToddSenior DBACommented:
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
 
shyamaladevibAuthor Commented:
Thanks!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now