2013-03-06 35 views
2

測試案例顯示錶的結構。我需要基於tab_2_3一個加入tab_1更新地址數據。更新腳本中所示的回報「缺少右括號」,我確信它指向的語法錯誤。希望得到任何幫助或指導,讓語句正確地更新基表。嘗試開發的語法從基於連接第三臺的另一個表更新值一個見下表

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

回答

2

你的前兩個update語句是不完整的 - 他們甚至不指定要更新的表。他們是那麼遙遠,我怕我會忽略它們,因爲我不明白他們怎麼是salvagable * 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); 
+0

「現代」連接語法一直在這裏自1996年以來!原始形式實際上是史前的。 – Bohemian 2013-03-06 20:30:05

+0

謝謝。奇蹟般有效 – 2013-03-06 21:15:16

相關問題