Home php教程 PHP开发 The use of cursors in SQL

The use of cursors in SQL

Dec 14, 2016 am 11:20 AM

Generally, when we use SELECT query statements, they are all for one row of records.
If you want to read multiple rows of records (i.e., recordset) in the query analyzer, you need to use a cursor Or WHILE and other loops


Types of cursors:
1. Static cursor (does not detect changes in data rows)
2. Dynamic cursor (reflects changes in all data rows)
3. Forward-only cursor (does not support scrolling)
4. Keyset cursor (can reflect modifications, but cannot accurately reflect insertions and deletions)

The order of cursor use:
1. Define the cursor
2. Open the cursor
3. Use the cursor
4. Close the cursor
5. Release the cursor

Transact-SQL:
declare cursor name cursor [LOCAL | GLOBAL][FORWARD_ONLY | SCROLL][STATIC | KEYSET | DYNAMIC] [READ_ONLY | SCROLL_LOCKS]
for selet statement [for update[of column name[, column name]]
Note: LOCAL local cursor GLOBAL global cursor
FORWARD_ONLY forward only SCROLL scroll
STATIC static KEYSET key set DYNAMIC dynamic
READ_ONLY read-only SCROLL_LOCKS lock the current row of the cursor

get the cursor data
FETCH PRI [[NEXT | OR | FIRST | LAST |
ABSOLUTE{ n | @nvar | RELATIVE { n | @nvar}]
From ] Cursor name [into variable]
Note:
NEXT Next line PRIOR Previous line FIRST First line
LAST Last line ABSOLUTE n Line n
RELATIVE n The nth row starting from the current position
into variable Assign each field value of the current row to the variable

Cursor status variable:
@@fetch_status Cursor status
Number of rows
                 N Number of rows - 1 The cursor is dynamic 0 Empty set cursor
Current row of the cursor:
current of Cursor name

Example 1: Use the cursor to traverse the queried data set

use pubs

go

declare @auid char(12),@aulname varchar(20),@aufname varchar(20), @st char(2),@auinfo varchar(50)

declare auth_cur cursor for

select au_id, au_lname, au_fname, state

from authors

open auth_cur

fetch next from auth_cur into @auid,@aulname,@aufname, @st

while (@@fetch_status=0)

begin

print 'Author number: ' +@auid

print 'Author's name: '+@aulname+','+@aufname

print 'State: '+@st

print '------------- -----------'

fetch next from auth_cur into @auid,@aulname,@aufname, @st

end

close auth_cur

deallocate auth_cur

Example 2: Use cursor to modify, Delete data

declare auth_cur cursor scroll for

select au_id, au_lname, au_fname, state

from authors for update of au_lname

open auth_cur

declare @rowcount int

set @rowcount = 6

fetch absolute @rowcount from auth_cur --Set the row identified by the variable @rowcount as the current row

--The following line is modified using the cursor

update authors set au_lname='Zhang' where current of auth_cur --Modify the current row in the cursor

--The following line is to use the cursor to perform a delete operation

delete from authors where current of auth_cur


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)