iT邦幫忙

0

oracle update 命令的結束有問題

我在eclipse java 想加這段程式

update
        (
            select
                t.a_id as pa_id,
                t.status as pa_status,
                s.a_id as pb_id  
            from
                product_a t,
                product_b s  
            where
                t.item_code = s.item_code
        ) 
    set
        pa_id = pb_id,
        pa_status = 'Y'; 

這段在eclipse出現ORA-00933: SQL 命令的結束有問題
我去查發現這個error只是語法有錯
但是我把這個toad oracle卻可以執行
請問各位大大到底是哪裡出現問題了呢

有檢查看看是不是多放了分號嗎:)
有試過了似乎不是這個問題,謝謝討論
再試一次,發現是分號的問題!!!!!感謝大大
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
rogeryao
iT邦超人 8 級 ‧ 2020-12-11 17:53:17
最佳解答

wow酷東西 謝謝你

加上primary key後可以正常執行,謝謝討論/images/emoticon/emoticon41.gif

1
純真的人
iT邦大師 1 級 ‧ 2020-12-11 18:28:13

你應該想要由b表資料更新a表吧~

https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=4110d0de5f4495dc58c8493b84558794

你試試

update product_a
    set
        a_id = (
            select product_b.a_id
            from product_b
            where product_a.item_code = product_b.item_code
        ) 
        ,status = 'Y'; 

好謝謝分享!

這樣status似乎是包不到where條件/images/emoticon/emoticon06.gif

1
pojen
iT邦研究生 5 級 ‧ 2020-12-13 11:45:20

句子本身沒問題, 只是給的資訊不夠. ORA-01779 是因為沒有 PK, 所以這個 join 無法確定需要更新的行. 以下利用 rogeryao 給的測試程序:

drop table product_a;
create table product_a(
  a_id varchar2(10) 
  ,status varchar2(50)
  ,item_code varchar2(50) primary key
);

insert into product_a(a_id,status,item_code) values('a','a','1');
insert into product_a(a_id,status,item_code) values('a','b','2');
insert into product_a(a_id,status,item_code) values('b','b','3');
insert into product_a(a_id,status,item_code) values('b','b','4');

select *
from product_a;

drop table product_b;
create table product_b(
  a_id varchar2(10) 
  ,item_code varchar2(50) primary key
);

insert into product_b(a_id,item_code) values('xx','1');
insert into product_b(a_id,item_code) values('yy','4');
insert into product_b(a_id,item_code) values('zz','5');

select *
from product_b;

select
    t.a_id as pa_id,
    t.status as pa_status,
    s.a_id as pb_id,
    t.item_code as pa_item_code,
    s.item_code as pb_item_code
from
    product_a t,
    product_b s  
where
    t.item_code = s.item_code;
    
update
        (
            select
                t.a_id as pa_id,
                t.status as pa_status,
                s.a_id as pb_id  
            from
                product_a t,
                product_b s  
            where
                t.item_code = s.item_code
        ) 
    set
        pa_id = pb_id,
        pa_status = 'Y'; 

select
    t.a_id as pa_id,
    t.status as pa_status,
    s.a_id as pb_id  
from
    product_a t,
    product_b s  
where
    t.item_code = s.item_code;

測試:
https://ithelp.ithome.com.tw/upload/images/20201213/20033353Vc5NfwwhDU.png

結果:
https://ithelp.ithome.com.tw/upload/images/20201213/20033353iKJ6zXEZB4.png

至於 ORA-00933, 我只能猜想 eclipse 裡的 Oracle driver 並不支援這種這種寫法.

至於原先的寫法有什麼好處, 因為原先版本的執行計畫像這樣:
https://ithelp.ithome.com.tw/upload/images/20201213/20033353XYkJ7ygXo0.png

純真的人 的執行計畫:
https://ithelp.ithome.com.tw/upload/images/20201213/20033353viMjHCj5AB.png

比較兩個執行計畫, 原先版的先執行了 join, 所以在 update 時能用索引, 另一個版本應為沒有(或者說不需) where, 所以他需要對原先的表格做一次 FTS. 你說哪個好, 那還真是看狀況. 原先版的需要滿足的條件太多, (就像是樓主遇到的問題, 明明句子沒錯, eclipse 就是不給過) 遇到這種狀況, 能用替代方案就上吧.

好我馬上來試試

這個寫法我之前好像試過,主要問題是我where條件是控制兩個column,
這樣寫status欄位似乎會無條件設置成'Y',感謝大大

我要發表回答

立即登入回答