2015-10-28 115 views
-1

這是MySQL代碼。將Mysql轉換爲Oracle

UPDATE ORDER_ITEM OI 
INNER JOIN ORDER_PAYMENT OP 
ON OI.ORDER_PAYMENT_ID = OP.ORDER_PAYMENT_ID 
SET OI.ORDER_STATUS = '10', 
    OI.PAY_DATE   = '20150101', 
    OP.PAY_DATE   = '20150101' 
WHERE OI.ORDER_STATUS  = '0' 
AND OP.AMOUNT    = 3333 
AND OP.REMAINING_AMOUNT  = 0 
AND OP.ORDER_PAYMENT_ID  = 9999 

轉換Oracle錯誤=> ORA-00971:缺少SET關鍵字

我想知道這種說法轉換到Oracle。

PS。 Oracle中可用的多表更新?

+1

http://www.geeksengine.com/article/oracle-cross-table-update.html –

回答

0

當然,Oracle不允許在一個SQL語句中更新兩個表。所以,你需要爲你的情況下,3個語句:

-- This query locks target rows in both tables. 
-- It's required to be sure that rows wasn't changed in other sessions 
-- between the first and second UPDATE statements 
SELECT * 
    FROM order_item oi 
INNER JOIN order_payment op 
    ON oi.order_payment_id = op.order_payment_id  
WHERE oi.order_status = '0' 
    AND op.amount = 3333 
    AND op.remaining_amount = 0 
    AND op.order_payment_id = 9999 
    FOR UPDATE NOWAIT 

-- first update 
UPDATE order_item 
    SET order_status = '10', 
     pay_date = '20150101' 
WHERE rowid IN (SELECT oi.rowid 
         FROM order_item oi 
        INNER JOIN order_payment op 
         ON oi.order_payment_id = op.order_payment_id         
        WHERE oi.order_status = '0' 
         AND op.amount = 3333 
         AND op.remaining_amount = 0 
         AND op.order_payment_id = 9999); 

--second update 
UPDATE order_payment 
    SET pay_date = '20150101' 
WHERE rowid IN (SELECT op.rowid 
         FROM order_item oi 
        INNER JOIN order_payment op 
         ON oi.order_payment_id = op.order_payment_id         
        WHERE oi.order_status = '0' 
         AND op.amount = 3333 
         AND op.remaining_amount = 0 
         AND op.order_payment_id = 9999); 

使用PL/SQL,我們可以簡化這個代碼一點點,但我們仍然必須寫一個SELECT FOR UPDATE以及每個連接表一個更新。