Oracle SQL:使用另一个表中的数据更新表

imzjd6km  于 2023-05-28  发布在  Oracle
关注(0)|答案(8)|浏览(346)

表1:

id    name    desc
-----------------------
1     a       abc
2     b       def
3     c       adf

表2:

id    name    desc
-----------------------
1     x       123
2     y       345

在oracle SQL中,如何运行一个sql update查询,可以使用表2的namedesc(使用相同的id)更新表1?所以我得到的最终结果是
表1:

id    name    desc
-----------------------
1     x       123
2     y       345
3     c       adf

问题取自update one table with data from another,但专门针对oracle SQL。

pprl5pva

pprl5pva1#

这称为相关更新

UPDATE table1 t1
   SET (name, desc) = (SELECT t2.name, t2.desc
                         FROM table2 t2
                        WHERE t1.id = t2.id)
 WHERE EXISTS (
    SELECT 1
      FROM table2 t2
     WHERE t1.id = t2.id )

假设连接产生保留键的视图,您还可以

UPDATE (SELECT t1.id, 
               t1.name name1,
               t1.desc desc1,
               t2.name name2,
               t2.desc desc2
          FROM table1 t1,
               table2 t2
         WHERE t1.id = t2.id)
   SET name1 = name2,
       desc1 = desc2
sr4lhrrt

sr4lhrrt2#

试试这个:

MERGE INTO table1 t1
USING
(
-- For more complicated queries you can use WITH clause here
SELECT * FROM table2
)t2
ON(t1.id = t2.id)
WHEN MATCHED THEN UPDATE SET
t1.name = t2.name,
t1.desc = t2.desc;
u4dcyp6a

u4dcyp6a3#

尝试

UPDATE Table1 T1 SET
T1.name = (SELECT T2.name FROM Table2 T2 WHERE T2.id = T1.id),
T1.desc = (SELECT T2.desc FROM Table2 T2 WHERE T2.id = T1.id)
WHERE T1.id IN (SELECT T2.id FROM Table2 T2 WHERE T2.id = T1.id);
kzipqqlq

kzipqqlq4#

Update table set column = (select...)

从来没有为我工作,因为集只需要1个值- SQL错误:ORA-01427:单行子查询返回多行。
解决方案如下:

BEGIN
For i in (select id, name, desc from table1) 
LOOP
Update table2 set name = i.name, desc = i.desc where id = i.id;
END LOOP;
END;

这就是在SQLDeveloper工作表上运行它的确切方式。他们说这很慢,但这是唯一的解决办法,为我工作的情况下。

vsmadaxz

vsmadaxz5#

这里似乎是一个更好的答案,使用'in'子句,允许连接多个键

update fp_active set STATE='E', 
   LAST_DATE_MAJ = sysdate where (client,code) in (select (client,code) from fp_detail
  where valid = 1) ...

完整的示例如下:http://forums.devshed.com/oracle-development-96/how-to-update-from-two-tables-195893.html-从Web存档,因为链接已失效。
问题在于,在where子句中,要用作键的列放在'in'前面的括号中,而select语句的列名放在括号中。where(column1,column2)in(select(column1,column2)from table where“the set I want”);

0h4hbjxa

0h4hbjxa6#

Oracle Database 23c为updatedelete添加了直接联接:

create table t1 (
  c1 int, c2 int
);
create table t2 (
  c1 int, c2 int
);

insert into t1 values ( 1, 1 ), ( 2, 2 );
insert into t2 values ( 1, 42 );

select * from t1;

        C1         C2
---------- ----------
         1          1
         2          2

update t1
set    t1.c2 = t2.c2
from   t2
where  t1.c1 = t2.c1;

select * from t1;

        C1         C2
---------- ----------
         1         42
         2          2

delete t1
from   t2
where  t1.c1 = t2.c1;

select * from t1;

        C1         C2
---------- ----------
         2          2
eoxn13cs

eoxn13cs7#

BEGIN
For i in (select id, name, desc from table2) 
LOOP
Update table1 set name = i.name, desc = i.desc where id = i.id and (name is null or desc is null);
END LOOP;
END;
ttygqcqt

ttygqcqt8#

如果你的表t1和它的备份t2有很多列,这里有一个紧凑的方法。
此外,我的相关问题是,只有一些列被修改,许多行没有对这些列进行编辑,所以我想不去管它们--基本上是从整个表的备份中恢复列的子集。如果只想恢复所有行,请跳过where子句。
当然,更简单的方法是删除和插入为选择,但在我的情况下,我需要一个解决方案,只是更新。
诀窍是,当你从一对具有重复列名的表中选择 * 时,第二个表将被命名为_1。所以我的想法是

update (
    select * from t1 join t2 on t2.id = t1.id
    where id in (
      select id from (
        select id, col1, col2, ... from t2
        minus select id, col1, col2, ... from t1
      )
    )
  ) set col1=col1_1, col2=col2_1, ...

相关问题