Cursor in SQL Server

8 Flares Filament.io 8 Flares ×

Cursor in SQL Server is a control structure which process data row by row. It enables rows in a result to process sequentially. SQL Cursor is a type of iterator which can be used to process any record of table & can modify records according to our needs.

To use cursors in SQL procedures, you need to do the following:

  • Declare a cursor that defines a result set.
  • Open the cursor to establish the result set.
  • Fetch the data into local variables as needed from the cursor, one row at a time.
  • Close and de-allocate the cursor when done.

For simple example of SQL Cursor, I used below example. I used table that I used in earlier post Delete Duplicate Rows in SQL.

In above example, I simple select column EMail of user table. I assign is to @EMailId variable. Then  Printing current row of SQL cursor one by one.!

ursor-example-in-SQL-server

SQL Cursor processes records one by one, so it consumes lots of memory & time. So use of cursor should be minimal.!

One thought on “Cursor in SQL Server

  1. Hey can you please give me example that how can i write php mysql store procedure ?
    i tried lot but i can not implement with one or more databse.i can do only for single DB.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">