Is it legal to update cursor's querying table in the loop of fetching data from it

妖精的绣舞 提交于 2019-12-11 20:33:17

问题


For example , is it ok to do below ?

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 

回答1:


Cursors in MySQL are 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.

- From DECLARE CURSOR Statement -

SQL Fiddle demo

However, depending on what you need to do, there are other ways to update the table.



来源:https://stackoverflow.com/questions/30365427/is-it-legal-to-update-cursors-querying-table-in-the-loop-of-fetching-data-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!