我希望根据以下条件使用OFFICE_ID,OFFICE_TYPE
表中的FA_SUBLEDGER_MST
来更新OFFICE_ID,OFFICE_TYPE
表的EMPLOYEE_MST
:
EMPLOYEE_MST.SL_CODE=FA_SUBLEDGER_MST.SL_CODE
EMPLOYEE_MST.OFFICE_ID<>SL.OFFICE_ID
OR EMPLOYEE_MST.OFFICE_TYPE<>SL.OFFICE_TYPE
AND EMPLOYEE_MST.OFFICE_TYPE!='DHB'.
我尝试了这个查询:
UPDATE FA_SUBLEDGER_MST sl
SET
(
sl.OFFICE_ID,sl.OFFICE_TYPE
)
=
(SELECT emp.OFFICE_ID,
emp.OFFICE_TYPE
FROM EMPLOYEE_MST emp
WHERE emp.OFFICE_ID<>sl.OFFICE_ID
OR emp.OFFICE_TYPE<>sl.OFFICE_TYPE
AND sl.SL_CODE = emp.SL_CODE
AND emp.OFFICE_TYPE!='DHB'
)
WHERE sl.STATUS = 'A'
AND EXISTS
(SELECT 1
FROM EMPLOYEE_MST emp
WHERE emp.OFFICE_ID<>sl.OFFICE_ID
OR emp.OFFICE_TYPE<>sl.OFFICE_TYPE
AND emp.SL_CODE=sl.SL_CODE
AND emp.OFFICE_TYPE!='DHB'
);
而且:
UPDATE FA_SUBLEDGER_MST
SET
(
OFFICE_ID,
OFFICE_TYPE
)
=
(SELECT OFFICE_ID,
OFFICE_TYPE
FROM EMPLOYEE_MST
WHERE FA_SUBLEDGER_MST.OFFICE_ID != EMPLOYEE_MST.OFFICE_ID
OR FA_SUBLEDGER_MST.OFFICE_TYPE! =EMPLOYEE_MST.OFFICE_TYPE
AND FA_SUBLEDGER_MST.SL_CODE = EMPLOYEE_MST.SL_CODE
AND EMPLOYEE_MST.OFFICE_TYPE! ='DHB'
) ;
但两者都造成了这个错误:
SQL错误:ORA-01427:单行子查询返回多行 01427. 00000 - “单行子查询返回多行
我也尝试过:
UPDATE
(SELECT em.OFFICE_ID emoffid,
SL.OFFICE_ID sloffid,
em.OFFICE_TYPE emofftype,
SL.OFFICE_TYPE slemofftype,
SL.STATUS
FROM EMPLOYEE_MST em
JOIN FA_SUBLEDGER_MST SL
ON em.SL_CODE =SL.SL_CODE
WHERE em.OFFICE_ID<>SL.OFFICE_ID
OR em.OFFICE_TYPE <>SL.OFFICE_TYPE
AND em.OFFICE_TYPE!='DHB'
AND SL.STATUS ='A'
) t
SET t.sloffid =t.emoffid
WHERE t.emoffid<>t.sloffid
OR t.emofftype <>t.slemofftype
AND t.emofftype!='DHB'
AND t.STATUS ='A';
这导致错误:
SQL错误:ORA-01779:无法修改映射到非密钥保留表的列
01779. 00000 - “无法修改映射到非密钥保留表的列”
*原因:尝试插入或更新连接视图的列,这些列映射到非密钥保留的表 *操作:直接修改基础表。
这两个表都包含超过100,000条记录,OFFICE_ID
可以是1,2,3,4,5
而OFFICE_TYPE
可以是'DE','DI','HO'
,因此它们会经常重复。
我需要一个Oracle查询来执行此操作。不能使用程序。
感谢你的帮助。
答案 0 :(得分:1)
尝试这个:
WHERE (FA_SUBLEDGER_MST.OFFICE_ID != EMPLOYEE_MST.OFFICE_ID AND
FA_SUBLEDGER_MST.OFFICE_TYPE !=EMPLOYEE_MST.OFFICE_TYPE) --AND will be converted to OR by SQL Engine
AND FA_SUBLEDGER_MST.SL_CODE = EMPLOYEE_MST.SL_CODE
AND EMPLOYEE_MST.OFFICE_TYPE! ='DHB'
AND sl.STATUS = 'A'
答案 1 :(得分:0)
尝试
UPDATE FA_SUBLEDGER_MST
SET sl.OFFICE_ID = em.OFFICE_ID
,sl.OFFICE_TYPE = em.OFFICE_TYPE
FROM FA_SUBLEDGER_MST sl
INNER JOIN EMPLOYEE_MST em ON em.SL_CODE =SL.SL_CODE
WHERE em.OFFICE_ID<>SL.OFFICE_ID
OR em.OFFICE_TYPE <>SL.OFFICE_TYPE
AND em.OFFICE_TYPE!='DHB'
AND SL.STATUS ='A'
答案 2 :(得分:0)
试试这段代码:
UPDATE FA_SUBLEDGER_MST
SET
(
OFFICE_ID,
OFFICE_TYPE
)
=
(SELECT OFFICE_ID,
OFFICE_TYPE
FROM EMPLOYEE_MST
WHERE .......
)
WHERE (FA_SUBLEDGER_MST.OFFICE_ID != EMPLOYEE_MST.OFFICE_ID AND
FA_SUBLEDGER_MST.OFFICE_TYPE !=EMPLOYEE_MST.OFFICE_TYPE)
AND FA_SUBLEDGER_MST.SL_CODE = EMPLOYEE_MST.SL_CODE
AND EMPLOYEE_MST.OFFICE_TYPE! ='DHB'
AND sl.STATUS = 'A' ;
答案 3 :(得分:0)
终于找到了解决方案:
merge into FA_SUBLEDGER_MST s1
using (
select SL_CODE, OFFICE_ID, OFFICE_TYPE
from EMPLOYEE_MST
where OFFICE_TYPE != 'DHB'
) emp
on (s1.SL_CODE = emp.SL_CODE)
when matched then update set
s1.OFFICE_ID = emp.OFFICE_ID,
s1.OFFICE_TYPE = emp.OFFICE_TYPE
where sl.STATUS = 'A'
and (s1.OFFICE_ID <> emp.OFFICE_ID
or s1.OFFICE_TYPE <> emp.OFFICE_TYPE)
;
Orignal Thread:Updating row of one table using rows of another table based on specific conditions on https://dba.stackexchange.com/