update table_name
set (col1,col2,col3)=(select t.col4,t.col5,t.col6 from table_name2 t where something)




OR

create table tgt ( id number /*primary key*/, x number, y number )
/
insert into tgt ( id, x, y )
select level, level, level from dual connect by level < 4
/
create table src ( id number /*primary key*/, x number, y number )
/
insert into src ( id, x, y )
select level+1, level*5, level*5 from dual connect by level < 3
/
commit
/
select * from src;

ID X Y
---------- ---------- ----------
2 5 5
3 10 10

select * from tgt;

ID X Y
---------- ---------- ----------
1 1 1
2 2 2
3 3 3
UPDATE tgt
SET ( tgt.x, tgt.y ) =
( SELECT src.x, src.y
FROM src
WHERE src.id = tgt.id
)
WHERE EXISTS
( SELECT src.x, src.y
FROM src
WHERE src.id = tgt.id
)
/

2 rows updated.

select * from tgt;

ID X Y
---------- ---------- ----------
1 1 1
2 5 5
3 10 10



OR

Provided that you have a unique constraint on join columns, you can directly update the view.
Adding PK to Guru's example :
ALTER TABLE src ADD CONSTRAINT pksrc PRIMARY KEY (id);

Update :
UPDATE
(SELECT tgt.id, tgt.x, tgt.y, src.x as srcx, src.y as srcy
FROM tgt
JOIN src ON tgt.id = src.id)
SET x = srcx, y = srcy;

SQL> select * from tgt;

ID X Y
---------- ---------- ----------
1 1 1
2 5 5
3 10 10

(But if you do not have the constraint, you get "not key preserved table" error)

0 comments:

Popular Posts

Blogger templates

Blogger news

Blogroll

Powered by Blogger.

Labels

Followers

Follow us on FaceBook