我的查询一直返回ORA-01427错误,我不知道如何解决它。
update db1.CENSUS set (notes)
=
(
select notes
from db2.CENSUS cen
where db1.CENSUS.uid = cen.uid
)
where headcount_ind = 'Y' and capture_FY = '2015'
答案 0 :(得分:2)
您收到错误是因为db2.CENSUS
中存在多行至少uid
的值。 (可能会有更多。)您可以通过执行以下操作来确定uid
的哪些值导致问题:
SELECT uid, COUNT(*)
FROM db2.census
GROUP BY uid
HAVING COUNT(*) > 1;
此时你可以做很多事情。您可以删除额外的行(可能没有那么多并且您不想要它们)并在原始查询中更新,或者您可以在您用于更新的子查询中使用聚合,例如:
update db1.CENSUS set (notes)
=
(
select MAX(notes)
from db2.CENSUS cen
where db1.CENSUS.uid = cen.uid
)
where headcount_ind = 'Y' and capture_FY = '2015';
此外,根据您的查询方式,如果notes
中db2.CENSUS
的某个值db1.CENSUS.uid
中没有相应的db1.CENSUS.notes
值,NULL
将设置为UPDATE db1.census c1
SET c1.notes = ( SELECT max(c2.notes)
FROM db2.census c2
WHERE c2.uid = c1.uid )
WHERE c1.headcount_ind = 'Y'
AND c1.capture_FY = '2015'
AND EXISTS ( SELECT 1 FROM db2.census c2
WHERE c2.uid = c1.uid );
。也许这就是你想要的行为?如果没有,你会想要以下内容:
{{1}}
答案 1 :(得分:0)
快速而肮脏
update db1.CENSUS cen set cen.notes
=
(
select MAX(notes)
from db2.census census
where db1.DW_HRT_PERSONNEL_CENSUS.uid = census.uid
)
where cen.headcount_ind = 'Y' and cen.capture_FY = '2015'
但是,这可能不会做你想要的。您有一个db1.DW_HRT_PERSONNEL_CENSUS成员输入的多个注释。
为了防止出现此错误,您必须知道表之间的关系。这是db1.DW_HRT_PERSONNEL_CENSUS的一个成员映射到一个注释,或者更可能的是,db1.DW_HRT_PERSONNEL_CENSUS的一个成员可以制作许多人口普查说明吗?更可能的解决方案是:
CURSOR census_taker IS
SELECT db1.DW_HRT_PERSONNEL_CENSUS.UID
FROM db1.DW_HRT_PERSONNEL_CENSUS;
For each census_taker in db1.DW_HRT_PERSONNEL_CENSUS LOOP
update db1.CENSUS cen set cen.notes
=
( select census.notes
from db2.census census
where db1.DW_HRT_PERSONNEL_CENSUS.uid = census.uid
)
where cen.headcount_ind = 'Y'
and cen.capture_FY = '2015'
and cen.PERSONNEL_CENSUS_UID = census_taker.UID;
END LOOP;
答案 2 :(得分:0)
错误表示子查询
select notes
from db2.CENSUS census
where db1.CENSUS.uid = census.uid
返回带有注释的多行,而UPDATE语句则期望返回单个值。
请尝试以下操作。
update db1.CENSUS set (notes) = (select notes
from db2.CENSUS census
inner join db1.CENSUS db1Census on db1Census.uid = census.uid
where db1Census.headcount_ind = 'Y'
and db1Census.capture_FY = '2015')
where headcount_ind = 'Y' and capture_FY = '2015'
如果db2.CENSUS
uid
和headcount_ind = 'Y' and capture_FY = '2015'
答案 3 :(得分:0)
您还可以在子查询中使用分析功能,例如
RANK()(通过census.uid ORDER BY 1 DESC划分)
然后选择第一等级或第二等级
答案 4 :(得分:0)
您的更新查询期望内部查询解析为单个记录,但它实际上是一个数组。如果内部查询中的每个结果具有相同的值,则可以执行以下操作:
update A a set a.x = (select b.x from B b where ... and ROWNUM=1)
E.g。
update db1.CENSUS set (notes)
=
(
select notes
from db2.CENSUS cen
where db1.CENSUS.uid = cen.uid
and ROWNUM=1
)
where headcount_ind = 'Y' and capture_FY = '2015'