【问题标题】:How to update Columns if value is not NULL如果值不为 NULL,如何更新列
【发布时间】:2017-01-09 12:25:23
【问题描述】:

如果从 C# 输入的值不是 NULL,我想更新表 TBL_Log 的任何列。这是我的存储过程:

Alter PROCEDURE [dbo].[SP_Update_User] 
(@User_id as int,@User_Names as nvarchar(max),@End as char(8),@Start as nvarchar(max) ,@Count as int)

AS
BEGIN


UPDATE [dbo].[TBL_Log]


     SET User_Names = @User_Names
      ,[Start] = @start
      ,[End] = @End
      ,[Count] = @Count
      where User_id = @User_id



END

我尝试过完成这项工作,但没有成功。

D1 类代码:

public static DataSet Update_User(int @User_id, string @User_Names, string @End, string @Start, int @Count)
    {

        SqlConnection myConnection = new SqlConnection(strConecctionString);


        SqlDataAdapter sqlcmd = new SqlDataAdapter("SP_Update_UserData_Bot", myConnection);
        sqlcmd.SelectCommand.CommandType = CommandType.StoredProcedure;


        SqlParameter parameterID_category_ID = new SqlParameter("@User_id", SqlDbType.Int);
        parameterID_category_ID.Value = User_id;
        sqlcmd.SelectCommand.Parameters.Add(parameterID_category_ID);

        SqlParameter parameterID_Part_ID = new SqlParameter("@User_Names", SqlDbType.Int);
        parameterID_Part_ID.Value = User_Names;
        sqlcmd.SelectCommand.Parameters.Add(parameterID_Part_ID);


        SqlParameter parameterID_Series_ID = new SqlParameter("@End", SqlDbType.Char);
        parameterID_Series_ID.Value = End;
        sqlcmd.SelectCommand.Parameters.Add(parameterID_Series_ID);

        SqlParameter parameterID_Model_ID = new SqlParameter("@start", SqlDbType.NVarChar);
        parameterID_Model_ID.Value = start;
        sqlcmd.SelectCommand.Parameters.Add(parameterID_Model_ID);

        SqlParameter parameterID_Count = new SqlParameter("@Count", SqlDbType.Int);
        parameterID_Count.Value = Count;
        sqlcmd.SelectCommand.Parameters.Add(parameterID_Count);


        sqlcmd.SelectCommand.CommandTimeout = int.MaxValue;
        DataSet DS = new DataSet();
        sqlcmd.Fill(DS);


        return DS;

    }

【问题讨论】:

  • 您的问题不清楚。您是说如果 C# 中提供的 @End 值不是 null,那么您想要将列 EndNULL 的所有行更新为 @End,并且对于所有其他参数也是如此?参数是独立的,即@Start 只影响StartNULL 的行,而不管@EndEnd?
  • 不是您问题的答案,而是您应该阅读的文章。 sqlperformance.com/2012/10/t-sql-queries/sp_prefix

标签: sql sql-server tsql null sql-update


【解决方案1】:

这将仅更新非空值。如果值为 null,则该列将更新回它自己的值。

UPDATE [dbo].[TBL_Log]
SET    User_Names = isnull(@User_Names, User_Names)
     , [Start] = isnull(@start, [Start])
     , [End] = isnull(@End, [End])
     , [Count] = isnull(@Count, [Count])
where User_id = @User_id

【讨论】:

    【解决方案2】:

    您应该展示您的 .Net 代码。添加检查的最佳位置可能是在 .NET 中,如果您担心的值为 NULL,请不要调用存储过程。

    您还必须指定哪个值不应为空,但假设您的意思是其中任何一个,您都可以这样做:

    IF (@User_Names IS NULL OR @start IS NULL OR @End IS NULL OR @Count IS NULL OR @User_Id IS NULL)
    BEGIN
        RETURN
    END
    

    如果任何参数为空而不更新表,则将退出存储过程

    给定您的 c# 代码,您可以在值为 null 时不调用存储过程或引发异常。您还应该考虑使用DateTime 而不是字符串作为日期值。

    您可以在您的 c# 代码中执行以下操作:

    if (@User_Names == null || @End == null || @Start == null)
      return;
    

    最好是

    if (@User_Names == null || @End == null || @Start == null)
      throw new ArgumentNullException();
    

    您甚至可以单独检查每个参数并将其名称作为参数传递给异常以给出有意义的错误消息。

    【讨论】:

    • 我想例如:@start = 20170910 和其他值为 null 。其他时间输入2个值进行更新,其他值为空。
    • 添加了c#答案
    【解决方案3】:

    您注释掉的逻辑很好:

    UPDATE [dbo].[TBL_Log]
         SET User_Names = @User_Names,
             [Start] = @start,
             [End] = @End,
             [Count] = @Count
          where User_id = @User_id and
                (@User_Names is not null and
                 @AkharinBazdid is not null and
                 @Pid_Bazdid  IS NOT NULL and
                 @C_Bazdid IS NOT NULL
                );
    

    如果您愿意,也可以使用IF

    注意事项:

    • 不要使用 SQL 关键字和保留字作为列名。转义字符会妨碍您。
    • 如果startend 是日期/时间,则使用适当的类型。不要使用字符串。

    【讨论】:

    • end 是日期时间 20171006,类型是 char(8)
    • 我们对@AkharinBazdid@Pid_Bazdid@C_Bazdid 等变量有什么假设,因为这些变量在给定的示例过程中似乎没有被定义为变量或传入参数?我们认为这只是来自 OP 的不完整的 sn-p,还是我遗漏了什么?
    • Gordon Linoff,不工作,没有更新,因为也许我想例如:@start = 20170910 和其他值为 null。其他时间输入2个值进行更新,其他值为空。
    猜你喜欢
    • 2012-10-29
    • 1970-01-01
    • 2017-12-13
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多