Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 views
in Technique[技术] by (71.8m points)

sql - Update multiple table column values using single query

How would you update data in multiple tables using a single query?

MySQL Example

The equivalent code in MySQL:

UPDATE party p
LEFT JOIN party_name n ON p.party_id = n.party_id
LEFT JOIN party_details d ON p.party_id = d.party_id
LEFT JOIN incident_participant ip ON ip.party_id = p.party_id
LEFT JOIN incident i ON ip.incident_id = i.incident_id
SET
  p.employee_id = NULL,
  c.em_address = '[email protected]',
  c.ad_postal = 'x',
  n.first_name = 'x',
  n.last_name = 'x'
WHERE
  i.confidential_dt IS NOT NULL

What would be the same statement using Oracle 11g?

Thank you!

RTFM

It seems a single query is insufficient when using Oracle:

http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_108a.htm#2067717

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
/** XXX CODING HORROR... */

Depending on your needs, you could use an updateable view. You create a view of your base tables and add an "instead of" trigger to this view and you update the view directly.

Some example tables:

create table party (
    party_id integer,
    employee_id integer
    );

create table party_name (
    party_id integer,
    first_name varchar2(120 char),
    last_name varchar2(120 char)
    );

insert into party values (1,1000);   
insert into party values (2,2000);
insert into party values (3,3000);

insert into party_name values (1,'Kipper','Family');
insert into party_name values (2,'Biff','Family');
insert into party_name values (3,'Chip','Family');

commit;

select * from party_v;

PARTY_ID    EMPLOYEE_ID    FIRST_NAME    LAST_NAME
1            1000           Kipper        Family
2            2000           Biff          Family
3            3000           Chip          Family

... then create an updateable view

create or replace view party_v
as
select
    p.party_id,
    p.employee_id,
    n.first_name,
    n.last_name
from
    party p left join party_name n on p.party_id = n.party_id;

create or replace trigger trg_party_update
instead of update on party_v 
for each row
declare
begin
--
    update party
    set
        party_id = :new.party_id,
        employee_id = :new.employee_id
    where
        party_id = :old.party_id;
--
    update party_name
    set
        party_id = :new.party_id,
        first_name = :new.first_name,
        last_name = :new.last_name
    where
        party_id = :old.party_id;
--
end;
/

You can now update the view directly...

update party_v
set
    employee_id = 42,
    last_name = 'Oxford'
where
    party_id = 1;

select * from party_v;

PARTY_ID    EMPLOYEE_ID    FIRST_NAME    LAST_NAME
1            42             Kipper        Oxford
2            2000           Biff          Family
3            3000           Chip          Family

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...