SQL更新具有不同列值条件的列值

发布于 2025-02-05 12:56:02 字数 482 浏览 3 评论 0原文

我有一个带有下面列的SQL表。我想根据其他超级磁极列的值更新最终目标列。如果有超级磁极,则应使用superAdminrole value更新final -lole coluation,logrole列值else

Groupid   OwnerGid  SuperAdminGroupId   groupRole   ownerRole  SuperAdminRole  FInalRole
-----   ----------   ----------------   ---------   ---------  --------------  ---------
17     20             3                  Admin       User       SAdmin      Sadmin

2       null          null               Admin                               Admin

I have a sql table with below columns .I want to update the finalRole column based on value from other Superadminrole column .If there is superadminrole, finalrole column should be updated with superadminrole value else with grouprole column value

Groupid   OwnerGid  SuperAdminGroupId   groupRole   ownerRole  SuperAdminRole  FInalRole
-----   ----------   ----------------   ---------   ---------  --------------  ---------
17     20             3                  Admin       User       SAdmin      Sadmin

2       null          null               Admin                               Admin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天气好吗我好吗 2025-02-12 12:56:02

您的 案例 表达式应如下:

UPDATE TableName SET 
       FinalRole = CASE WHEN ISNULL(SuperAdminRole, '') = '' THEN groupRole 
                        WHEN ISNULL(SuperAdminRole, '') != '' THEN SuperAdminRole 
                        ELSE '' END

或者您可以使用 iif 逻辑表达式:

UPDATE TableName SET 
       FinalRole = IIF(ISNULL(SuperAdminRole, '') != '', SuperAdminRole, groupRole) 

Your CASE expression should be as below:

UPDATE TableName SET 
       FinalRole = CASE WHEN ISNULL(SuperAdminRole, '') = '' THEN groupRole 
                        WHEN ISNULL(SuperAdminRole, '') != '' THEN SuperAdminRole 
                        ELSE '' END

or you may use IIF logical expression:

UPDATE TableName SET 
       FinalRole = IIF(ISNULL(SuperAdminRole, '') != '', SuperAdminRole, groupRole) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文