T-SQL:在 UPDATE 语句中使用 CASE 根据条件更新某些列
我想知道这是否可能。如果条件为真,我想更新列 x,否则列 y 将被更新
UPDATE table SET
(CASE (CONDITION) WHEN TRUE THEN columnx
ELSE columny
END)
= 25
我已经搜索遍了,尝试了一些东西,但无法找到解决方案。我认为这是不可能的,但我想我应该在这里问一下,看看以前是否有人这样做过。
I am wondering if this is possible at all. I want to update column x if a condition is true, otherwise column y would be updated
UPDATE table SET
(CASE (CONDITION) WHEN TRUE THEN columnx
ELSE columny
END)
= 25
I have searched all over, tried out some things and am unable to find a solution. I think it's not possible, but I thought I would ask here and see if anyone has done it before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不能使用条件来更改查询的结构,只能更改所涉及的数据。您可以这样做:
这在语义上是相同的,但请记住两列都将始终更新。这可能不会给您带来任何问题,但如果您的事务量很大,那么这可能会导致并发问题。
实现您所要求的具体的唯一方法是使用动态SQL。然而,我鼓励您远离这种情况。上面的解决方案几乎肯定足以满足您的需求。
You can't use a condition to change the structure of your query, just the data involved. You could do this:
This is semantically the same, but just bear in mind that both columns will always be updated. This probably won't cause you any problems, but if you have a high transactional volume, then this could cause concurrency issues.
The only way to do specifically what you're asking is to use dynamic SQL. This is, however, something I'd encourage you to stay away from. The solution above will almost certainly be sufficient for what you're after.
我知道这是一个非常古老的问题,但这对我有用:
I know this is a very old question, but this worked for me:
我想使用 Case 语句将我的 ContactNo 更改或更新为 8018070999,其中有 8018070777
I want to change or update my ContactNo to 8018070999 where there is 8018070777 using Case statement
我知道这是一个非常古老的问题,并且该问题已标记为已修复。但是,如果有人遇到像我这样的情况,其中表具有更新事件数据记录的触发器,这将导致问题。两列都将获得更新,并且日志将生成无用的条目。我这样做的方式
现在还有另一个好处,那就是它不会像上面的解决方案那样在表上进行不必要的写入。
I know this is a very old question and the problem is marked as fixed. However, if someone with a case like mine where the table have trigger for data logging on update events, this will cause problem. Both the columns will get the update and log will make useless entries. The way I did
Now this have another benefit that it does not have unnecessary writes on the table like the above solutions.
我相信您可以通过调整其他答案来省略更新“不需要的”列,如下所示:
据我了解,只有满足条件时才会更新。
阅读所有评论后,这是最有效的:
示例表:
示例数据:
现在我假设您可以编写一个处理空值的条件。对于我的示例,我假设您已经编写了这样一个计算结果为 True、False 或 Null 的条件。如果您需要这方面的帮助,请告诉我,我会尽力而为。
现在运行这两行代码实际上确实将 X 更改为 25 当且仅当 ColConditional 为 True(1) 时,将 Y 更改为 25 当且仅当 ColConditional 为 False(0)
PS 原始问题或任何问题中从未提到过 null 情况更新了问题,但正如您所看到的,这个非常简单的答案无论如何都能处理它们。
I believe that you can omit updating the "non-desired" columns by adjusting the other answers as follows:
As I understand it, this will update only when the condition is met.
After reading all the comments, this is the most efficient:
Sample Table:
Sample Data:
Now I assume you can write a conditional that handles nulls. For my example, I am assuming you have written such a conditional that evaluates to True, False or Null. If you need help with this, let me know and I will do my best.
Now running these two lines of code does infact change X to 25 if and only if ColConditional is True(1) and Y to 25 if and only if ColConditional is False(0)
P.S. The null case was never mentioned in the original question or any updates to the question, but as you can see, this very simple answer handles them anyway.