set the result table name in stored procedure

2019-05-25 06:17发布

a stored procedure that return multiple results:

CREATE PROCEDURE [dbo].[GetMultipleTable]
AS
BEGIN

   if exists (select something from somewhere where somecondition = 1)
   begin
       select * from firsttable
   end
   select * from secondtable
END

when we execute the stored procedure, use SqlDataAdapter's Fill method to fill a DataSet with multiple tables.

DataSet ds;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

I can get the results. but I think we can do better than this:

var index = 0;
if( ds.Tables.Count > 1 ){
   DataTable first = ds.Tables[index];       
   index++;
   // do something
}
DataTable second = ds.Tables[index];
// do something 

I'm curious if we can get table by name instead of index. like..

DataTable first = ds.Tables["first"];
// do something
if(ds.Tables.Containts("second"))
{
    DataTable second = ds.Tables["second"];
    // do something
}

the question is: is it possible to set the name of the result table in stored procedure?

1条回答
霸刀☆藐视天下
2楼-- · 2019-05-25 06:40

I guess not. And to some extent I doubt that it is a good way to handle this. Why not just return empty result sets in those cases, than you always know what the nth result set means.

Besides cases where you render the results to plain text, don't you need complicated logic, do deal with the different cases of the result.

Why are you doing it? A mystic wish to improve performance ?

For ad hoc queries it might be OK, but for stored procedures. Better think twice.

Perhaps my attitude is influenced by the fact, that I had to convert such procedures to Oracle and there you do not have the concept of a variable number of result sets. For each result set you have to provide a refcursor parameter in advance.

查看更多
登录 后发表回答