这是mysql代码。
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中提供的多表更新?
答案 0 :(得分: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和一个UPDATE。