沒有必要使用PL/SQL,這可以在SQL中使用ROW_NUMBER
解析函數來完成:
SELECT ORD_ID,
ROW_NUMBER() OVER (PARTITION BY ORD_ID
ORDER BY ITEM) -- or ORDER BY ROWNUM
AS ai_ln,
ITEM
FROM customer_order;
如果要更新,則表:
UPDATE customer_table c
SET ai_ln = (
SELECT rn
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY ord_id
ORDER BY item)
AS rn
FROM customer_table
) r
WHERE c.ROWID = r.ROWID
);
或者MERGE
:
MERGE INTO customer_table dst
USING (SELECT ROW_NUMBER() OVER (PARTITION BY ord_id
ORDER BY item) AS rn
FROM customer_table) src
ON (dst.ROWID = src.ROWID)
WHEN MATCHED THEN
UPDATE SET ai_ln = src.rn;
來源
2017-04-09 09:46:49
MT0
糾正我,如果我錯了,但更新與自相關查詢表,這是一個昂貴的,不是嗎?我認爲這將循環更有效率。 – Seyran
@Seyran如果你正在談論一個PL/SQL循環,那麼上下文切換將更加快速。 – MT0