我在 Oracle 中加入的更新语句有什么问题?
问题描述
我正在使用 Oracle 10g 数据库.
I am working with an Oracle 10g Database.
我有以下两张表:
这两个表使用 ID_DEBTOR 字段连接.
These two tables are joined using the ID_DEBTOR field.
只有当 HEADER 不为空时,我才想用关联的 T_DEBTOR.HEADER 更新 T_ELEMENT.INSURER 值.换句话说:
I want to update the T_ELEMENT.INSURER value with the associated T_DEBTOR.HEADER only if HEADER is not null. In others words:
我尝试使用以下 SQL 查询:
I tried to use the following SQL query:
此查询适用于与 HEADER 不为空的债务人相关联的所有元素.但是,当 T_DEBTOR.HEADER 为 null 时,此查询将 T_ELEMENT.INSURER 设置为 null,这是不正确的.
This query is working for all elements linked to debtors that has a HEADER not null. However, when the T_DEBTOR.HEADER is null, then this query set the T_ELEMENT.INSURER to null, which is not correct.
即:
我的查询有什么问题?
编辑,关于 Brian Storrar 的回答:
我想做的是这样的:
推荐答案
好问题.
为了模拟您的情况,我创建了示例表:
To simulate your situation, I've created sample tables:
使用您当前的更新语句,问题变得清晰:不更新"值设置为 NULL:
And with your current update statement, the problem becomes clear: the "not to be updated" values are set to NULL:
进行此更新的最佳方法是更新两个表的连接.但是有一些限制:
The best way to do this update, is to update a join of both tables. There are some restrictions however:
通过绕过 ujvc 提示,我们可以绕过这个限制.但除非您确实确定 t_debtor.id_debtor 是独一无二的,否则不建议这样做.
With the bypass ujvc hint, we can circumvent this restriction. But it is not advisable to do so unless you know really really sure that t_debtor.id_debtor is unique.
最好只添加一个主键.你可能已经有了这个:
It's better to just add a primary key. You'll probably have this one already in place:
问候,抢.
这篇关于我在 Oracle 中加入的更新语句有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!