I have a stored procedure that returns multiple resultsets, it looks something like this
BEGIN
SET NOCOUNT ON;
SELECT c1, c2, c3
FROM t1
WHERE id = @id
IF (@@ROWCOUNT = 0)
BEGIN
SELECT c1, c2, c3
FROM t2
WHERE id = @id
END
END
I use this stored procedure in my front-end ASP.NET website.
If the first SELECT statement did not return any rows I get 2 resultsets (1st one is obviously empty) in my SqlDataReader. Is there a way to return only the resultset from the last SELECT statement?
Couple of options you could take here, you’d have to test in your environment to see which works best.
First option would be to wrap the first statement in an if block similar to what you’ve done with the second block:
Second option would be to use a temp table/variable: