2012-07-29 101 views
1

這可能是一個非常簡單的問題,但我自己找不到答案:MySQL存儲過程:我可以直接用光標更新嗎?

當我有一個指向我想要更新的行的遊標時,是否有比發出UPDATE語句更快的方法?

DECLARE current_id, current_product_id, current_price, current_position INT DEFAULT 0; 
DECLARE new_position INT DEFAULT 0; 
DECLARE cursor_offers CURSOR FOR 
    SELECT id, product_id, price, position FROM offers 
    ORDER BY product_id, price ASC; 

OPEN cursor_offers; 
offers_loop: LOOP 
    FETCH cursor_offers INTO current_id, current_product_id, current_price, current_position; 
    # Loop control omitted 
    # Conditional statements omitted, that calculate new_position 

    UPDATE offers SET position = new_position WHERE id = current_id; # <--- This line 
END LOOP offers_loop; 

我估計,像MySQL已經有一個指針,通過遊標線,這將是低效的UPDATE語句再次找到它。

+0

據我所知,遊標是結果集,而不是表本身。此外還有更多更新行,而不僅僅是知道它在哪裏。 – Vatev 2012-07-29 13:04:46

回答

3

閱讀the documentation。它表示遊標是隻讀的,不能用於更新。

所以我會說這是不可能使用遊標直接更新。你將不得不發佈更新聲明。

相關問題