Call Another stored procedure which returns muliple rows

  • Hi,

    I am using sql server 2005,In that we have one stored procedure A to call stored procedure B, which returns a set of records from a table. Then, I want stored procedure A to perform work on these records. How do I call stored procedure B from within stored procedure A?

    Thanks in advance.

  • Sorry do not understand your question ... why not reverse the calling order of your 2 procedures. That is call procedure "B" first to insert the data into either a temporary table or a table variable and then operate on that data with procedure "A".

    If you follow the example given by clicking on the first link in my signature block you will most likely receive the assistance you are requesting.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Sorry for the uncleared question.Here I will Call stored procedure 'B' from 'A' by passing one parameter.Based on the parameter value ,the Stored procedure B will retrun data multiple rows to A.

  • amalanto in that case you have to create a table which will capture the results inside the stored procedure; so you need to know the structure and datatypes returned;something like this:

    create procedure myproc

    as

    begin

    create table #tmp(tmpid int, columnlist.....)

    insert into #tmp(tmpid,columnlist.....)

    EXEC myotherProc

    --do more stuff to the contents of #tmp

    end

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thank you sir.

    Right now We have achieved by calling UDF.I want to know whether is it poassible through stored procedure.

    Create Function dbo.Result(@TranId int)

    Returns @t Table(@col1 varchar(10),@col2 Datetime)

    Begin

    Insert into @t select Status,Date from TransHistory where Transactionid=@TranId

    return

    end

  • amalanto (2/25/2010)


    Thank you sir.

    Right now We have achieved by calling UDF.I want to know whether is it poassible through stored procedure.

    Create Function dbo.Result(@TranId int)

    Returns @t Table(@col1 varchar(10),@col2 Datetime)

    Begin

    Insert into @t select Status,Date from TransHistory where Transactionid=@TranId

    return

    end

    not enough info; your function above returns a table of values based on a single Transactionid;

    we need to know what the "outer" procedure is doing to help you; is it calling this function multiple times?

    if it is, you should remove the function and do a JOIN in the "outer" procedure instead. you never want to use a function to get similar data multiple times.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • amalanto (2/25/2010)


    Hi,

    I am using sql server 2005,In that we have one stored procedure A to call stored procedure B, which returns a set of records from a table. Then, I want stored procedure A to perform work on these records. How do I call stored procedure B from within stored procedure A?

    Thanks in advance.

    I think you can use this logic and try this works as the same way you asked B to A

    in the below Eg only one output has been given in your case you can use your records in the OUTPUT.

    USE AdventureWorks;

    GO

    IF OBJECT_ID ( 'Production.usp_GetList', 'P' ) IS NOT NULL

    DROP PROCEDURE Production.usp_GetList;

    GO

    CREATE PROCEDURE Production.usp_GetList @product varchar(40)

    , @maxprice money

    , @compareprice money OUTPUT

    , @listprice money OUT

    AS

    SELECT p.name AS Product, p.ListPrice AS 'List Price'

    FROM Production.Product p

    JOIN Production.ProductSubcategory s

    ON p.ProductSubcategoryID = s.ProductSubcategoryID

    WHERE s.name LIKE @product AND p.ListPrice < @maxprice;

    -- Populate the output variable @listprice.

    SET @listprice = (SELECT MAX(p.ListPrice)

    FROM Production.Product p

    JOIN Production.ProductSubcategory s

    ON p.ProductSubcategoryID = s.ProductSubcategoryID

    WHERE s.name LIKE @product AND p.ListPrice < @maxprice);

    -- Populate the output variable @compareprice.

    SET @compareprice = @maxprice;

    GO

    DECLARE @compareprice money, @cost money

    EXECUTE Production.usp_GetList '%Bikes%', 700,

    @compareprice OUT,

    @cost OUTPUT

    IF @cost <= @compareprice

    BEGIN

    PRINT 'These products can be purchased for less than

    $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'

    END

    ELSE

    PRINT 'The prices for all products in this category exceed

    $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'

    Thanks
    Parthi

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply