gpt4 book ai didi

MySQL 存储过程 : Can I update directly with the cursor?

转载 作者:行者123 更新时间:2023-11-29 02:02:58 26 4
gpt4 key购买 nike

这可能是一个非常简单的问题,但我自己找不到答案:

当我有一个光标指向我想要更新的行时,有没有比发出 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 语句中再次找到它是低效的。

最佳答案

阅读the documentation .它说游标是只读的,不能用于更新。

所以我想说使用游标直接更新是不可能的。您必须发布更新声明。

关于MySQL 存储过程 : Can I update directly with the cursor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11707893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com