O'Reilly logo

SQL in a Nutshell, 3rd Edition by Kevin Kline

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

FETCH Statement

The FETCH statement is one of four commands used in cursor processing, along with DECLARE, OPEN, and CLOSE. Cursors allow you to process queries one row at a time, rather than as a complete set. FETCH positions a cursor on a specific row and retrieves that row from the result set.

Cursors are especially important in relational databases because they are set-based, while most client-centric programming languages are row-based. Cursors allow you to perform operations a single row at a time, to better fit what a client program can do, rather than operating on a whole set of records at once.

Platform

Command

MySQL

Supported, with limitations

Oracle

Supported, with limitations

PostgreSQL

Supported, with variations

SQL Server

Supported, with variations

SQL2003 Syntax

FETCH [ { NEXT | PRIOR | FIRST | LAST |
  { ABSOLUTE int | RELATIVE int } }
  FROM ] cursor_name
[INTO variable1[, ...]]

Keywords

NEXT

Tells the cursor to return the record immediately following the current row, and increments the current row to the row returned. FETCH NEXT is the default behavior for FETCH. It retrieves the first record if FETCH is performed as the first fetch against a cursor.

PRIOR

Tells the cursor to return the record immediately preceding the current row and decrements the current row to the row returned. FETCH PRIOR does not retrieve a record if it is performed as the first fetch against the cursor.

FIRST

Tells the cursor to return the first record in the cursor result set, making it the current row.

LAST ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required