本文介绍了子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在项目上工作时遇到错误,无法找到我的错误...

While Working on a project, facing with a error, unable to find my mistake...

Create PRoc uspCheckSubscription
@Userid int,@HasSubscrip bit out, @SubscripID int out,@HasSubscripLife bit out
AS
BEGIN
IF EXISTS (Select SubscriptionId from TblSubscription where UserId = @Userid and IsActive = 1)
BEGIN
Set @HasSubscrip = 1
Set @SubscripID = (Select SubscriptionId from TblSubscription where UserId = @Userid and IsActive = 1)
IF EXISTS (Select SubscriptionId from TblSubscription where UserId = @Userid and IsActive = 1 and GETDATE() between StartDate and FinishDate)
BEGIN
SET @HasSubscripLife = 1
END
ELSE
BEGIN
SET @HasSubscripLife = 0
UPDATE TblSubscription SET IsActive = 0 WHERE UserId = @Userid and IsActive = 1
END
END
ELSE
BEGIN
Set @HasSubscrip = 0
SET @HasSubscripLife = 0
END
END







private void GetSuscriptionetials(int Userid)
    {
        int SubscripID;
        Boolean HasSubscrip, HasSubscripLife;
        try
        {
            db1.sqlcmd = new SqlCommand("uspCheckSubscription");
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                db1.sqlcmd.Parameters.AddWithValue("@Userid", Userid);
                db1.sqlcmd.Parameters.Add("@HasSubscrip", SqlDbType.Bit);
                db1.sqlcmd.Parameters.Add("@HasSubscripLife", SqlDbType.Bit);
                db1.sqlcmd.Parameters.Add("@SubscripID", SqlDbType.Int);
                db1.sqlcmd.Parameters["@HasSubscrip"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters["@HasSubscripLife"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters["@SubscripID"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Connection = db1.sqlcon;
                db1.sqlcon.Open();
                int success = Convert.ToInt32(db1.sqlcmd.ExecuteScalar());
                HasSubscrip = Convert.ToBoolean(db1.sqlcmd.Parameters["@HasSubscrip"].Value);
                HasSubscripLife = Convert.ToBoolean(db1.sqlcmd.Parameters["@HasSubscripLife"].Value);
                SubscripID = Convert.ToInt32(db1.sqlcmd.Parameters["@SubscripID"].Value);
                ViewState["SubscripID"] = SubscripID;
                ViewState["HasSubscrip"] = HasSubscrip;
                ViewState["HasSubscripLife"] = HasSubscripLife;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally {
            db1.sqlcon.Close();
        }
    }

推荐答案

Set @SubscripID = (Select TOP 1 SubscriptionId from TblSubscription where UserId = @Userid and IsActive = 1)





在where子句中使用更多条件来返回子查询中的单值和右值。



Use more conditions in where clause to return single and right value in a subquery.


这篇关于子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 05:11