Stored Procedure return multiple result sets

前端 未结 2 1720
庸人自扰
庸人自扰 2020-12-10 04:57

I need a SP to return multiple sets of results. The second set of results would be based on a column of the first set of results.

So:

declare @myTabl         


        
相关标签:
2条回答
  • 2020-12-10 05:46

    You pretty much just select two result sets

    SELECT * FROM @myTable1
    SELECT * FROM @myTable2
    

    However, some tools will hide some results (e.g. pgAdmin will only show the last) and some tools have some sort of requirement to get to the next result set (e.g. .NET's IDataReader's will not allow you to Read() from the second resultset until you call NextResult()).

    Edit:

    An alternative in this case, since the types of the two results match, is to combine them into a single resultset:

    SELECT field0, field1 from @myTable1
    UNION
    SELECT field0, field3 from @myTable2
    

    You can also choose between UNION ALL or UNION DISTINCT (the default) where the latter will only send rows that aren't repeats.

    0 讨论(0)
  • 2020-12-10 05:50

    At the end of the Stored Proc, put:

    SELECT * FROM @myTable1
    SELECT * FROM @myTable2
    

    This will return 2 result sets.

    0 讨论(0)
提交回复
热议问题