两个选择查询在一个存储过程中

时间:2012-03-22 12:13:36

标签: asp.net sql

我想从1个存储过程中选择两个查询....我的以下查询是否正确?以及如何在2 gridview中调用它

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           id=@id

1 个答案:

答案 0 :(得分: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