Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when should we use sql cursor

Tags:

tsql

hi all tell me when should i use cursor?

thanks saj

like image 330
selvaraj Avatar asked Feb 27 '23 09:02

selvaraj


1 Answers

Ideally as little as possible. Cursors have quite a high inherent processing overhead

Either when

  1. The process you are doing can not be rewritten as a set based operation. (For example invoking DBCC DBREINDEX in turn on a list of tables)
  2. The set based operation has worse asymptotic complexity. For running aggregates the set based solution has quadratic complexity whereas the cursor workload grows linearly.

For this last case using a SQL CLR solution is significantly faster than using a standard cursor.

like image 134
Martin Smith Avatar answered Mar 02 '23 17:03

Martin Smith