SQL过程中的更新语句
我只是想知道,更新语句是如何工作的?例如,SQL 在更新语句期间做了什么?我目前正在理解以下脚本。
UPDATE Employees
SET
EmployeeLeaves -= 1,
IsOnLeave = CASE WHEN (EmployeeLeaves > 0) THEN 1 ELSE 0 END
WHERE
EmployeeNo = 2000;
在 IsOnLeave
语句中,这是否意味着如果“前一个”EmployeeLeaves
大于 1,那么 IsOnleave
将更新为 1?
I just want to know, how the update statement works? Like, what does SQL do during the update statement? I'm currently understanding the following script.
UPDATE Employees
SET
EmployeeLeaves -= 1,
IsOnLeave = CASE WHEN (EmployeeLeaves > 0) THEN 1 ELSE 0 END
WHERE
EmployeeNo = 2000;
In the IsOnLeave
statement, does this mean that if the "previous" EmployeeLeaves
is greater than 1 then the IsOnleave
will be updated to 1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短的回答是是,它将得到 1 或 0,但大多数 sql 实现会在更新之前使用 EmployeeLeaves 的值,然后再设置新值。这看起来很合理,因为操作是原子的。
The short answer is yes it will get 1 or 0 but most sql implementations would use the value of EmployeeLeaves before the update before the setting of the new value. It seems reasonable since the operation in atomic.
差不多,但不完全对。无论如何,IsOnLeave 列都会更新;如果 EmployeeLeaves > 则变为 1 0,否则为 0。但是,是的,考虑的是原始记录(更新之前)的 EmployeeLeaves 值。
Almost, but not quite, right. The IsOnLeave column will be updated no matter what; it will become 1 if EmployeeLeaves > 0, otherwise 0. But yes, it is the value of
EmployeeLeaves
of the original record (before the update) that is considered.是的,SQL UPDATE 中赋值右侧的所有内容均指更新之前的记录值。
Yes, everything on the right hand side of an assignment in an SQL UPDATE refers to the value of the record before the update.