Sunday, 1 January 2012

Multiple select statements in a stored procedure sql server 2005,2008

Yeah its possible..what we need is to specify the multiple select statements in store procedure..there is lots of way to fetch data. here i m using dataset to fetch all select statement...


ALTER PROCEDURE dbo.MultiSelect

AS
SELECT   count(1) as totaluser  FROM    USER_MAS;
/* SET NOCOUNT ON */
SELECT   count(1) as totalcomp FROM   COMPANY_MASTER
RETURN




private void GetMultiSelect()
        {
            using (SqlConnection con = new SqlConnection("ConnectionString"))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = "MultiSelect";
                    cmd.CommandType = CommandType.StoredProcedure;

                    //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
                        ds.Tables[0]; // first select statement result
                        ds.Tables[1]; // second select statement result
                    }
                }
            }
        }

No comments:

Post a Comment