Postgresql 中文操作指南

DECLARE

DECLARE — 定义一个游标

DECLARE — define a cursor

Synopsis

DECLARE cursor_name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ] CURSOR [ { WITH | WITHOUT } HOLD ] FOR prepared_name
DECLARE cursor_name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ] CURSOR [ { WITH | WITHOUT } HOLD ] FOR query

Description

DECLARE 为迭代准备语句的结果集声明一个游标。此命令的语义与直接 SQL 命令 DECLARE 稍有不同:后者执行一个查询并准备结果集以进行检索,而此嵌入式 SQL 命令仅将一个名称声明为“循环变量”,以便迭代查询的结果集;实际执行在使用 OPEN 命令打开游标时发生。

DECLARE declares a cursor for iterating over the result set of a prepared statement. This command has slightly different semantics from the direct SQL command DECLARE: Whereas the latter executes a query and prepares the result set for retrieval, this embedded SQL command merely declares a name as a “loop variable” for iterating over the result set of a query; the actual execution happens when the cursor is opened with the OPEN command.

Parameters

  • cursor_name #

    • A cursor name, case sensitive. This can be an SQL identifier or a host variable.

  • prepared_name #

    • The name of a prepared query, either as an SQL identifier or a host variable.

  • query #

    • A SELECT or VALUES command which will provide the rows to be returned by the cursor.

有关游标选项的含义,请见 DECLARE

For the meaning of the cursor options, see DECLARE.

Examples

声明查询游标的示例:

Examples declaring a cursor for a query:

EXEC SQL DECLARE C CURSOR FOR SELECT * FROM My_Table;
EXEC SQL DECLARE C CURSOR FOR SELECT Item1 FROM T;
EXEC SQL DECLARE cur1 CURSOR FOR SELECT version();

声明准备语句游标的示例:

An example declaring a cursor for a prepared statement:

EXEC SQL PREPARE stmt1 AS SELECT version();
EXEC SQL DECLARE cur1 CURSOR FOR stmt1;

Compatibility

DECLARE 在 SQL 标准中指定。

DECLARE is specified in the SQL standard.

See Also