2012-03-22 70 views
1

我想从一个存储过程中选择两个查询....我的下面的查询是正确的?以及如何在GridView中2调用此在一个存储过程中的两个select查询

create procedure tablename 
@id varchar(50) 
as 
select a.id, 
isnull(a.advance,0) as advance, 
isnull(b.submitted_amt,0) as submitted 
into #temp1 
from 
(select id,SUM(Amount)as advance 
from table1 where type='Travel' group by id) a 
full join 
(select id,SUM(Amount)as submitted_amt 
from table2 group by categeoryid) b 
on a.id=b.id 
select id, 
advance, 
submitted, 
advance-submitted as balance_return 
from #temp1 
select categeoryid,advance,submitted,advance-submitted as balance_return from #temp1   [email protected] 

回答

1

请使用此示例:

private void GetMultiSelect() 
{ 
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString)) 
    { 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.Connection = con; 
      cmd.CommandText = "AllDetail"; 
      cmd.CommandType = CommandType.StoredProcedure; 
      SqlParameter sqlParam = cmd.Parameters.Add("@Id", SqlDbType.Int, 4); 
      sqlParam.Value = 1; 
      //dataset object to get all select statement results 
      DataSet ds = new DataSet(); 

      //sql dataadoptor to fill dataset 
      using (SqlDataAdapter adp = new SqlDataAdapter(cmd)) 
      { 
       //here all select statements are fill in dataset object 
       adp.Fill(ds); 

       //now u can fetch each and every select statement by providing table index in dataset 

       foreach (DataTable dt in ds.Tables) 
       { 
        //select statement result in dt.. 
       } 

       //or instead of loop u can specify the index 
       GridView1.DataSource= ds.Tables[1]; // first select statement result 
       GridView1.DataBind(); 
       GridView2.DataSource = ds.Tables[0]; // second select statement result 
       GridView2.DataBind(); 
      } 
     } 
    } 
} 

Please go to this link for details

相关问题