I need receive multiple recordsets at the same time. Can a stored procedure do this?
Thank for your help !
I need receive multiple recordsets at the same time. Can a stored procedure do this?
Thank for your help !
A stored procedure can return as many resultsets as you want. These are difficult to process in t-sql but easy with the ADO.Net datareader NextResult method.
Thank for your answer.
But I want to know more detail.
For example, I have a stored procedure like this:
CREATE PROCEDURE storename
(params)
AS
BEGIN
/* Process blocks /
/ Results saved in 2 temp table /
/ I want to return both temp tables by using:
SELECT * FROM #temp1
SELECT * FROM #temp2
Is this OK?
*/
END
In .NET, I'll a ADO.NET DataReader to receive them (with NextResult method), won't I?
Yes, definitely OK
I don't know ADO.NET, but the concept of "Next Result Set" is definitely how you "get" to the second (and subsequent) resultsets.
We have a centralised function to do the "Next resultset thing" which includes handling these scenarios:
It also handles "I got an error, along with a resulset for the Error Message(s)", and things like
SELECT MAX(MyCol)
FROM MyTable
where MyCol has NULL values and this statement returns a WARNING message about that (that too can interfere with the whole "Next Resultset" process )
Put
SET NOCOUNT ON
at the top of (inside) your SProc. Otherwise all the "1 row(s) affected)" messages will also get in the way
And if you have any PRINT statements (e.g. for Debugging) they can muck things up too ...
Anyway, our "GetNextResultSet()
" function takes care of handling all those situations, so I recommend centralising that bit of your code so you can "improve" it over time as you fall into each of these pitfalls.
maybe ADO.NET includes solutions for all those GotChas without you having to write a line of code?!!