假如有一张人员表sys_user(10000人)
| id |
| name |
| age |
和一张临时表sys_temp(100人)
| id |
| name_new |
| age |
现在要将临时表中的name_new 字段的值更新到sys_user的name字段中去,用两表的id作为关联条件:
错误的sql
update sys_user a set name=(select name_new from sys_temp b where a.id = b.id)
原因:在oracle中,如果update后边没有where条件,会把整个表全部更新,那么不符合子查询规则的数据,会将name字段的变成空。
正确sql
可以在后边加上exists条件来进行限制
update sys_user a
set name=(select name_new from sys_temp b where a.id = b.id)
where exists(select 1 from sys_temp temp where temp.id = a.id );
本文解析了在Oracle数据库中,如何正确地使用SQL语句更新大型表中的特定记录,避免因子查询规则不当导致的数据错误更新。
1701

被折叠的 条评论
为什么被折叠?



