尝试开发语法以基于第三个表的连接使用来自另一个表的值更新一个表

时间:2013-03-06 19:39:11

标签: sql oracle oracle11g

下面的测试用例显示了表的构造。我需要根据tab_1tab_2上的联接更新_3中的地址数据。显示的更新脚本返回'缺少右括号',我确信它指出语法中的错误。非常感谢您获取声明以正确更新基表的任何帮助或指导。

create table tab_1(address varchar2(25), city varchar2(25), state varchar2(2),zip varchar2(10), office_id varchar2(25));

create table tab_2 (company varchar2(25), office varchar2(25), address_id varchar2(5), office_id varchar2(5));

create table tab_3 (address_id varchar2(5), address varchar2(25), city varchar2(25), state varchar2(2),zip varchar2(10));


insert into tab_1(office_id) values(46);

insert into tab_2(company, office, address_id, office_id) 
    values('Stone', 'north', '45', '15');
insert into tab_3(address_id, address, city, state, zip) 
    values('15', '12Main', 'York', 'NY', '12345');


ALTER TABLE TAB_1 ADD 
CONSTRAINT tab_1_PK
 PRIMARY KEY (OFFICE_ID)
 ENABLE
 VALIDATE;

 ALTER TABLE TAB_2 ADD 
CONSTRAINT tab_2_PK
 PRIMARY KEY (OFFICE_ID)
 ENABLE
 VALIDATE;

ALTER TABLE TAB_3 ADD 
CONSTRAINT tab_3_PK
 PRIMARY KEY (ADDRESS_ID)
 ENABLE
 VALIDATE;




update (select tab_3.address, tab_3.city, tab_3.state, tab_3.zip, tab_1.address,     tab_1.city, tab_1.state, tab_1.zip
        FROM
    INNER JOIN tab_1 ON (tab_1.office_id=tab_2.office.id) 
    INNER JOIN tab_3 ON (tab_2.address_id = tab_3.address_id))
        SET tab_1.address=tab_3.address, tab_1.city=tab_3.city, tab_1.state=tab_3.state, tab_1.zip=tab_3.zip;


UPDATE ( SELECT src.x src_x, src.y src_y , tgt.x tgt_x, tgt.y tgt_y FROM src 
INNER JOIN tgt ON ( src.id = tgt.id ) ) SET tgt_x = src_x , tgt_y = src_y

*******************************************************

UPDATE tab_1
   SET (address,
        city,
        state,
        zip) =
          (SELECT (address, city, state, zip)
             FROM tab_3, tab2
            WHERE     tab_1.office_id = tab_2.office_id
                  AND tab_2.address_id = tab_3.address_id);

1 个答案:

答案 0 :(得分:2)

您的前两个update语句不完整 - 它们甚至没有指定要更新的表。他们离我们太远了我害怕我会忽略它们,因为我看不出它们是如何被拯救的* 8 - )

你的第三个作为一组额外的括号;你不需要它们(并且它们无效)在你在子查询中选择的列列表周围,它试图将其解释为不存在的另一个子查询。您在其中一个表名中也有拼写错误:

UPDATE tab_1
   SET (address, city, state, zip) =
       (SELECT address, city, state, zip
          FROM tab_3, tab_2
         WHERE tab_1.office_id = tab_2.office_id
           AND tab_2.address_id = tab_3.address_id);

我建议您使用现代join语法,特别是如果这是相当新的并且您尚未学习(可论证)坏习惯:

UPDATE tab_1
   SET (address, city, state, zip) =
       (SELECT address, city, state, zip
          FROM tab_2
          JOIN tab_3
            ON tab_3.address_id = tab_2.address_id
         WHERE tab_2.office_id = tab_1.office_id);