从 union all 查询返回多个结果集
returning multiple results set from union all query
我有这个 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());
}
}
我认为这不是检查用户是否拥有许可证的好方法.. 是否有其他选择
有什么方法可以简单地 return 来自 SP 的每个结果集的代码.. 如果我想从所有查询中获取所有计数,我是否需要修改 DAL 中的任何代码...
返回这些结果的另一种方法是在您的 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......
}
我有这个 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());
}
}
我认为这不是检查用户是否拥有许可证的好方法.. 是否有其他选择
有什么方法可以简单地 return 来自 SP 的每个结果集的代码.. 如果我想从所有查询中获取所有计数,我是否需要修改 DAL 中的任何代码...
返回这些结果的另一种方法是在您的 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......
}