在从中获取数据的循环中更新游标的查询 table 是否合法
Is it legal to update cursor's querying table in the loop of fetching data from it
例如,下面的操作可以吗?
DECLARE aId VARCHAR(20);
DECLARE cur1 CURSOR FOR SELECT id FROM new_records WHERE is_loaded = false;
read_loop: LOOP
FETCH cur1 INTO aId;
...
update new_records set is_loaded = True where id = aId ;
...
CLOSE cur1;
END
MySQL 中的游标是 ASENSITIVE (13.6.6 Cursors)。
An INSENSITIVE Cursor is a Cursor that effectively causes a separate
copy of its result Table to be created; the Cursor accesses that copy,
rather than the original result, so any changes made to the original
result by other methods won't be visible to this Cursor. A SENSITIVE
Cursor is a Cursor that works directly on its result Table: it makes
no copy, so other changes made to the result Table will be visible to
this Cursor. An ASENSITIVE Cursor may or may not make a copy of its
result Table; whether other changes to its result Table will be
visible is implementation-defined. The default is an ASENSITIVE
Cursor.
- 来自 DECLARE CURSOR Statement -
但是,根据您需要执行的操作,还有其他方法可以更新 table。
例如,下面的操作可以吗?
DECLARE aId VARCHAR(20);
DECLARE cur1 CURSOR FOR SELECT id FROM new_records WHERE is_loaded = false;
read_loop: LOOP
FETCH cur1 INTO aId;
...
update new_records set is_loaded = True where id = aId ;
...
CLOSE cur1;
END
MySQL 中的游标是 ASENSITIVE (13.6.6 Cursors)。
An INSENSITIVE Cursor is a Cursor that effectively causes a separate copy of its result Table to be created; the Cursor accesses that copy, rather than the original result, so any changes made to the original result by other methods won't be visible to this Cursor. A SENSITIVE Cursor is a Cursor that works directly on its result Table: it makes no copy, so other changes made to the result Table will be visible to this Cursor. An ASENSITIVE Cursor may or may not make a copy of its result Table; whether other changes to its result Table will be visible is implementation-defined. The default is an ASENSITIVE Cursor.
- 来自 DECLARE CURSOR Statement -
但是,根据您需要执行的操作,还有其他方法可以更新 table。