简体   繁体   English

用另一列的条件更新列

[英]update column with condition from another column

I am trying to update a column based on the condition from another column. 我正在尝试根据另一列的条件更新列。 something like this, > 像这样的东西,>

Attire app | 服装应用| SPV id | SPV ID | spv app spv应用

  | test | | test | | | | | 

expected: 预期:

Attire App | 服装应用| Spv id | 身份证号| SPV App SPV应用

approved | 已批准| test | 测试

approved | 已批准| test | 测试

approved | 已批准| | | approved 批准

approved | 已批准| | | approved 批准

so, when column 2 is not null then update column 1, if column 2 is null then update column 1 and column 3. 因此,如果列2不为空,则更新列1,如果列2为空,则更新列1和列3。

here is my code : 这是我的代码:

        strcommand = "update tbl_Approve set [Attire App] = 'Approved', [Attire Date] = @ADate, [SPV app] = case when [spv id] is null then 'Approved' END where [attire app] is null"

on the visual I didn't add one other column named [attire date], assume that there is that column in the visual. 在视觉上,我没有添加另一列名为[着装日期]的列,假设视觉上有该列。 but it doesn't work, it's only update column 1... do you have any suggestion about this problem? 但它不起作用,仅更新第1列...您对此问题有任何建议吗?

thanks in advance. 提前致谢。

I can't follow your code because it uses different column names from the description. 我无法遵循您的代码,因为它使用了与描述不同的列名。 However, the logic that you want is: 但是,您想要的逻辑是:

update yourtable
    set col1 = @COL1,
        col3 = (case when col2 = '' or col2 is null then @COL3 else col3 end);

Notice the else in the case statement. 注意case语句中的else This sets the value of the column to itself -- so the value doesn't change. 这会将列的值设置为其自身-因此值不会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM