2

下面的测试用例显示了表的构造。我需要tab_1根据tab_2和上的连接来更新地址数据_3。显示的更新脚本返回'missing right parenthesis',我确信它指向语法错误。在获得正确更新基表的语句方面提供任何帮助或指导。

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);
4

1 回答 1

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);
于 2013-03-06T19:57:28.733 回答