As in the topic. Can I simply reset cursor's position to the beginning in Transact-SQL, so it can run again over the table? I want to reset it in the following context:
DECLARE @userID INT
DECLARE user_cursor CURSOR FOR SELECT userID FROM users
WHILE /* some condition */
BEGIN
...
FETCH NEXT FROM user_cursor INTO @userID
IF @@FETCH_STATUS = 0
BEGIN
/*... here goes the reset of the cursor ...*/
END
...
END
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:
But the
scroll
option will be cheaper in terms of resource usage, if that's a change you can accept.you need to declare your cursor as scroll, like this
then at any time for locating to the first just use the following
Use cursor loops and its taken care of for you...
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.