2012-07-29 100 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。它表示游标是只读的,不能用于更新。

所以我会说这是不可能使用游标直接更新。你将不得不发布更新声明。

相关问题