Retrieve data from stored procedure which has multiple result sets

后端 未结 9 564
一个人的身影
一个人的身影 2020-11-30 08:47

Given a stored procedure in SQL Server which has multiple select statements, is there a way to work with those results separately while calling the procedure?

9条回答
  •  忘掉有多难
    2020-11-30 09:23

    It seems like there's no good simple way to do this, without a hack or a major paradigm shift. It looks like the best way is to just split out the original procs and end up with one more proc than before:

    Old way:

    create procedure dbo.GetSomething
    as
    begin
        select * from dbo.Person;
        select * from dbo.Car;
    end;
    

    New way:

    create procedure dbo.GetPeople
    as
    begin
        select * from dbo.Person;
    end;
    
    create procedure dbo.GetCars
    as
    begin
        select * from dbo.Car;
    end;
    
    -- This gives the same result as before
    create procedure dbo.GetSomething
    as
    begin
        exec dbo.GetPeople;
        exec dbo.GetCars;
    end;
    

    Then when I'm in a different proc and need both result sets, I'd just have to call them one at a time.

提交回复
热议问题