There is another advantage to using the DECLARE @local_variable CURSOR syntax that I just discovered.
The advantage is when one stored procedure calls another, and both procedures open cursors at the same time. If DECLARE cursor_name CURSOR used to define cursors, and both procedures use the same cursor_name, then you get
Msg 16915: A cursor named "cursor_name" already exists.
On the other hand, if DECLARE @local_variable CURSOR used to define cursors in parent and child stored procedures, then @local_variable is local to each procedure and there is no conflict. For those who have not previously used this method, here is an example using @C as a local variable:
DECLARE @C AS CURSOR; SET @C = CURSOR FOR SELECT ...; OPEN @C; FETCH NEXT FROM @C INTO ...;
...
user2461186
source share