Command Prompt, Inc.



DECLARE -- Defines a new cursor.


DECLARE cursorname
    CURSOR FOR query
    [ FOR { READ ONLY | UPDATE [ OF column [, ...] ] } ]



The name of the new cursor.


The BINARY keyword causes the cursor to fetch data in binary format, rather than in the default text format.


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.


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.


The SQL query that will provide the new cursor with rows. For information on how to construct this query, see SELECT."


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.


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).


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).



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.


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.


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.


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;
booktown=# DECLARE cur_publisher CURSOR FOR SELECT name FROM publishers;
booktown=# FETCH FORWARD 2 IN cur_publisher;
 Kids Can Press
 Henry Holt & Company, Inc.
(2 rows)

Powered by Mammoth PostgreSQL
Copyright © 2000-2007 Command Prompt, Inc. All Rights Reserved. All trademarks property of their respective owners.