![](/img/trans.png)
[英]Can we update a record in a table and simultaneously insert the updated record in another table in Oracle DB 11g
[英]Update a table from another table on multiple columns in Oracle 11g
Oracle 11g SQL 和两个表具有相同的列定义:
VARCHAR2(11)
NUMBER
DATE
DATE
我试图找到解决这个问题的方法,这就是我最终得到的结果,但失败了:
update jjjTable
set [fourthCol] = B.[fourthOtherCol]
from jjjTable, otherTable B
where jjjTable.[firstCol] = B.[firstOtherCol]
and jjjTable.[secondCol] = B.[secondOtherCol]
and jjjTable.[thirdCol] = B.[thirdOtherCol]
我的印象是,我需要从这篇文章中获取信息:
基于 ID 匹配和来自 Shivkant 的编辑响应从一个表更新到另一个表
我的印象是我可能需要使用基于这篇文章的连接:
如何从 SQL Server 中的 SELECT 更新? 以及罗宾·戴的回应
但据我了解,每行只有一个列匹配。 我对匹配 3 个元素感兴趣,但我没有找到明确的解决方案。
任何方向都会受到好评。
这就是我最终需要做的解决方案:
DECLARE
CURSOR j_CUR IS
SELECT A.[fourthCol]
FROM JJJtable A, otherTable B
WHERE A.[firstCol] = B.[firstOtherCol]
and A.[secondCol] = B.[secondOtherCol]
and A.[thirdCol] = B.[thirdOtherCol]
FOR UPDATE OF B.[fourthOtherCol];
SOME_DATE DATE;
BEGIN
FOR IDX IN j_CUR LOOP
SOME_DATE :=(IDX.[fourthCol]);
UPDATE otherTable
SET [fourthOtherCol] = SOME_DATE
WHERE CURRENT OF j_CUR;
END LOOP;
END;
感谢您的努力和指导。
这是我能够让它在我的类似用例上工作的最接近的最好的。 试试这个。
update jjjTable
SET jjjTable.[fourthCol] = (SELECT distint otherTable.fourthOtherCol from otherTable
WHERE otherTable.firstOtherCol = jjjTable.firstCol and
otherTable.secondOtherCol = jjjTable.secondCol and
otherTable.thirdOtherCol = jjjTable.thirdCol)
WHERE EXISTS (SELECT distint otherTable.fourthOtherCol from otherTable
WHERE otherTable.firstOtherCol = jjjTable.firstCol and
otherTable.secondOtherCol = jjjTable.secondCol and
otherTable.thirdOtherCol = jjjTable.thirdCol);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.