Can I reset cursor's position to the beginning?

て烟熏妆下的殇ゞ 提交于 2019-11-30 08:11:26

you need to declare your cursor as scroll, like this

declare c scroll cursor for (select statement); 

then at any time for locating to the first just use the following

fetch first from c;
Damien_The_Unbeliever

Another option that can be used that doesn't force you to change the type of cursor is simply to close the cursor and re-open it:

CLOSE user_cursor
OPEN user_cursor

But the scroll option will be cheaper in terms of resource usage, if that's a change you can accept.

The data retrieved by the cursor will not change.

STATIC

Defines a cursor that makes a temporary copy of the data to be used by the cursor. All requests to the cursor are answered from this temporary table in tempdb; therefore, modifications made to base tables are not reflected in the data returned by fetches made to this cursor, and this cursor does not allow modifications.

matthew rapaport

Use cursor loops and its taken care of for you...

cursor c_something IS
   select * from somewhere

BEGIN

    for some_row in c_something LOOP
        -- do stuff with some_row.COLUMN;
    END LOOP; -- this closes the cursor for you when it has gone all the way through

    -- do other stuff

    for some_row in c_something LOOP -- opens the cursor again from the top
        -- do stuff with some_row.COLUMN;
    END LOOP;

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