从union all查询返回多个结果集

时间:2015-08-26 12:15:37

标签: sql sql-server sql-server-2008 sql-server-2005

我有这个SP来检查用户是否拥有存储在不同表中的任何许可证。

我将结果导入数据集,并且从该数据集中我获得了计数的单个结果,如果计数大于零,则该用户拥有许可证。

这是SP

ALTER PROCEDURE [dbo].[UserCheck]
(
@activatedBy varchar(30),
@brand varchar(20)
)
AS 
BEGIN 
   DECLARE @acctId as BIGINT
   SELECT @acctId = pk_acct_id from accounts with(nolock) where email = @activatedBy  and  brand = @brand

 IF LEN(@acctId) > 1
  BEGIN
     SELECT count(*) from dbo.links with(nolock) where one = @acctId
   union all 
     SELECT COUNT(*)FROM waveactivationinfo with(nolock) where Activated_by = @acctId    
   union all
      SELECT COUNT(*) FROM ABCActivationInfo with(nolock) WHERE Activated_by = @acctId
   union all
      SELECT COUNT(*) FROM CSE_ActivationInfo with(nolock) WHERE activated_by = @acctId
   union all
       SELECT COUNT(*) FROM Connect_ActivationInfo  with(nolock) WHERE activated_by = @acctId
   union all
       SELECT COUNT(*) FROM LicActivationInfo with(nolock) WHERE Activated_by = @acctId
   END 
END
GO

然后在DAL中我将结果捕获到像这样的数据集中

    public DataSet UserCheck(string strEmailID, string strBrand)
    {
        DataSet ds = new DataSet();
        List<SqlParameter> ParaList = new List<SqlParameter>();
        ParaList.Add(new SqlParameter("@activatedBy", strEmailID));
        ParaList.Add(new SqlParameter("@brand", strBrand));
        ds = SqlHelper.ExecuteDataset(new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString), CommandType.StoredProcedure, "UserCheck", Convert.ToInt32(Utility.GetConfigValue("Connection_TimeOut")), ParaList.ToArray());
        return ds;
    }

我正在这样的代码中检索该数据集......

 DataSet ds = userDeactivate.UserCheck(txtEmailID.Text.Trim(), brandType);


if (ds != null)
{
    if (ds.Tables[0].Rows.Count > 0)
    {
        osCount = Int32.Parse(ds.Tables[0].Rows[0].ItemArray[0].ToString());
        waveCount=Int32.Parse(ds.Tables[0].Rows[1].ItemArray[0].ToString());
        aCount = Int32.Parse(ds.Tables[0].Rows[2].ItemArray[0].ToString());
        PassCount = Int32.Parse(ds.Tables[0].Rows[3].ItemArray[0].ToString());
        quickCount = Int32.Parse(ds.Tables[0].Rows[4].ItemArray[0].ToString());
        vmcCount = Int32.Parse(ds.Tables[0].Rows[5].ItemArray[0].ToString());
    }
 } 

我认为这不是检查用户是否拥有许可证的好方法..有没有替代方案

有没有办法简单地从SP为每个结果集返回代码..如果我想从所有查询中获取所有计数,我是否需要修改DAL中的任何代码...

1 个答案:

答案 0 :(得分:0)

获得这些结果的另一种方法是在sp中使用输出参数,例如......

ALTER PROCEDURE [dbo].[UserCheck]
( @activatedBy      varchar(30)
 ,@brand            varchar(20)
 ,@Link_Count       INT         OUTPUT
 ,@Wave_Count       INT         OUTPUT
 ,@ABCA_Count       INT         OUTPUT
 ,@CSE_Count        INT         OUTPUT
 ,@Connection_Couny INT         OUTPUT
 ,@Lic_Count        INT         OUTPUT
)
AS 
BEGIN 
   DECLARE @acctId as BIGINT
   SELECT @acctId = pk_acct_id from accounts with(nolock) where email = @activatedBy  and  brand = @brand

 IF LEN(@acctId) > 1
  BEGIN
     SELECT @Link_Count = ISNULL(count(*), 0) from dbo.links with(nolock) where one = @acctId

     SELECT @Wave_Count = ISNULL(COUNT(*), 0) FROM waveactivationinfo with(nolock) where Activated_by = @acctId    

     SELECT @ABCA_Count = ISNULL(COUNT(*), 0)  FROM ABCActivationInfo with(nolock) WHERE Activated_by = @acctId

     SELECT @CSE_Count = ISNULL(COUNT(*), 0)  FROM CSE_ActivationInfo with(nolock) WHERE activated_by = @acctId

     SELECT @Connection_Couny = ISNULL(COUNT(*), 0)  FROM Connect_ActivationInfo  with(nolock) WHERE activated_by = @acctId

     SELECT @Lic_Count = ISNULL(COUNT(*), 0)  FROM LicActivationInfo with(nolock) WHERE Activated_by = @acctId
   END 
END
GO

从C#调用

using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["Connection"]))
{
    conn.Open();
    SqlCommand cmd = new SqlCommand("[dbo].[UserCheck]", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@Link_Count", SqlDbType.Int).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@Wave_Count", SqlDbType.Int).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@ABCA_Count", SqlDbType.Int).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@CSE_Count", SqlDbType.Int).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@Connection_Couny", SqlDbType.Int).Direction = ParameterDirection.Output;
    cmd.Parameters.Add("@Lic_Count", SqlDbType.Int).Direction = ParameterDirection.Output;
    cmd.Parameters.Add(new SqlParameter("@activatedBy", strEmailID));
    cmd.Parameters.Add(new SqlParameter("@brand", strBrand));

    cmd.ExecuteNonQuery();
    int _linkCount = Convert.ToInt32(cmd.Parameters["@Link_Count"].Value);
    int _WaveCount = Convert.ToInt32(cmd.Parameters["@Wave_Count"].Value);
    // and so on......
}