?
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
Medium Priority
?
424 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 1000 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

771 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