|
|
|
|
NameDECLARE -- Defines a new cursor.
Synopsis
DECLARE
cursorname
[ BINARY ] [ INSENSITIVE ] [ SCROLL ]
CURSOR FOR
query
[ FOR { READ ONLY | UPDATE [ OF
column
[, ...] ] } ]
Parameters
-
cursorname
-
The name of the new cursor.
-
BINARY
-
The BINARY keyword causes the cursor to fetch data in binary format, rather than in the default text format.
-
INSENSITIVE
-
The INSENSITIVE keyword specifies that all data retrieved from the cursor will be unchanged by updates from other processes (and other cursors). This option is unneeded when using PostgreSQL, as the database already encapsulates all cursor operations within transactions. This option exists for compatibility with other database systems.
-
SCROLL
-
The SCROLL keyword allows data to be retrieved in multiple rows per FETCH operation. However, specifying it will have no effect, as PostgreSQL already allows this functionality implicitly.
-
query
-
The SQL query that will provide the new cursor with rows. For information on how to construct this query, see SELECT."
-
READ ONLY
-
The READ ONLY clause indicates that the cursor will be used only to read data (read-only mode). Using this keyword has no effect, as PostgreSQL already only provides read-only access for use with cursors.
-
UPDATE
-
The UPDATE clause specifies that the cursor will be used to update tables; however, updates from cursors are not supported as of PostgreSQL 7.1.x (the current version at the printing of this book).
-
column
-
The columns to be updated; however, cursor updates are not currently supported as of PostgreSQL 7.1.x (the current version at the printing of this book).
Results
-
SELECT
-
The message returned when a SELECT statement executes successfully.
-
NOTICE: Closing pre-existing portal "
cursorname
"
-
The notice returned when a cursor with the name you specified has already been declared within the current transaction block. If this happens, the previously declared cursor is automatically discarded.
-
ERROR: DECLARE CURSOR may only be used in begin/end transaction blocks
-
The error returned if you attempt to declare a cursor outside of a transaction block. You must be within a transaction block to use cursors.
Description
Use the DECLARE command to create a cursor within a transaction block, which can then be used to retrieve data from queries. Returned data can be in either text or binary format. The use of cursors is only supported within transaction blocks. You will receive an error if you attempt to use them without starting a transaction block.
Warning
|
Use binary cursors with caution, as not all clients support their use.
|
PostgreSQL does not require you to explicitly open a cursor; the cursor is opened when you declare it. However, the use of explicit OPEN commands is supported by the preprocessor,
ecpg
, for use with embedded or interactive SQL applications.
Example
The following example declares a cursor named cur_publisher and then uses that cursor to fetch 2 rows. Used directly within
psql
, these results are immediately displayed:
booktown=#
BEGIN WORK;
BEGIN
booktown=#
DECLARE cur_publisher CURSOR FOR SELECT name FROM publishers;
SELECT
booktown=#
FETCH FORWARD 2 IN cur_publisher;
name
----------------------------
Kids Can Press
Henry Holt & Company, Inc.
(2 rows)
|
|
|