Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to actually step within your loop, you do this by repeating the fetch inside the loop:</p> <pre><code>FETCH NEXT FROM MyCursor INTO @a WHILE @@FETCH_STATUS = 0 BEGIN -- Some operations FETCH NEXT FROM MyCursor INTO @a ----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END </code></pre> <p>Otherwise, it's an infinite loop, constantly processing the first row.</p> <p>As an aside, a cursor is very rarely the right answer; perhaps you should consider trying a set-based solution instead of a cursor. If you really need a cursor, at the very least, do this:</p> <pre><code>DECLARE MyCursor CURSOR LOCAL FAST_FORWARD FOR </code></pre> <p>If the query is spinning and spinning, then it's probably being blocked. This could be due to the cursor options, so trying <code>LOCAL FAST_FORWARD</code> may help, but it's probably more than that. We can't guess what you're being blocked by but you can check for yourself. Stop the query, and in that window, do <code>SELECT @@SPID;</code> and note the number that is returned. Now, start the cursor query again, and open a new window. Run the following (replacing <code>x</code> with the <code>@@SPID</code> number):</p> <pre><code>SELECT blocking_session_id, wait_type FROM sys.dm_exec_requests WHERE session_id = x; </code></pre> <p>Now, if you get another session id in the first column, you can see what they are doing and why they are blocking you, using a similar query and a <code>DBCC</code> command:</p> <pre><code>SELECT status, command FROM sys.dm_exec_requests WHERE session_id = y; DBCC INPUTBUFFER(y); </code></pre> <p>(Again, replacing <code>y</code> with the session id from the previous query.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload