[Database Video] Understanding of the cursor

Overview

What is a cursor

The cursor is a data access mechanism that allows users to access individual data rows instead of operating on the entire row

Benefits of using cursors

  • Process each line separately and collect information one by one
  • Operate the data row by row

Why use cursors

  • Reduce system overhead
  • Reduce potential barriers
  • Generate SQL code and execute or output immediately

The composition of the cursor

  • Cursor result set
    • The collection of rows returned by the SELECT statement that defines the cursor
  • Cursor position
    • Pointer to a row in this result set

Features of cursor

  1. Return a complete result set, allowing the programming language to call only one line in the set
  2. Allow positioning in a specific row of the result set
  3. Retrieve one or more rows from the current position of the result set
  4. Support for data modification of the current row in the result set
  5. Can be changed to provide different levels of visibility support
  6. Provide T-SQL statements used in scripts, stored procedures and triggers to access data in the result set

Grammar format

 

How to operate the cursor

step

  • Open cursor
    • OPEN
  • Search cursor
    • FETCH
  • Close cursor
    • CLOSE
  • Release cursor
    • DEALLOCATE

Determine the cursor extraction status

variable

@@FETCH_STATUS

  • Value
    • 0 FETCH statement succeeded
    • -1 FETCH statement failed or the row is not in the result set
    • -2 The extracted row does not exist 

 

Guess you like

Origin blog.csdn.net/weixin_45490023/article/details/110229003