> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-sql-commands/ecpg-sql-declare.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# DECLARE

### DECLARE

DECLARE — 定義游標

### 語法

```

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
```

### 說明

`DECLARE` 會宣告一個游標，用以逐一走訪預備陳述式的結果集。此命令的語意與直接 SQL 命令 `DECLARE` 略有不同：後者會執行查詢並準備結果集以供擷取，而此嵌入式 SQL 命令只是將一個名稱宣告為逐一走訪查詢結果集的「迴圈變數」；實際的執行發生在以 `OPEN` 命令開啟游標時。

### 參數

#### *`cursor_name`* [#](#ecpg-sql-declare-cursor-name) <a href="#ecpg-sql-declare-cursor-name" id="ecpg-sql-declare-cursor-name"></a>

游標名稱，區分大小寫。可以是 SQL 識別字或主機變數。

#### *`prepared_name`* [#](#ecpg-sql-declare-prepared-name) <a href="#ecpg-sql-declare-prepared-name" id="ecpg-sql-declare-prepared-name"></a>

預備查詢的名稱，可以是 SQL 識別字或主機變數。

#### *`query`* [#](#ecpg-sql-declare-query) <a href="#ecpg-sql-declare-query" id="ecpg-sql-declare-query"></a>

提供游標所要傳回之資料列的 [SELECT](https://docs.postgresql.tw/19/reference/sql-commands/sql-select.md) 或 [VALUES](https://docs.postgresql.tw/19/reference/sql-commands/sql-values.md) 命令。

游標選項的意義請參閱 [DECLARE](https://docs.postgresql.tw/19/reference/sql-commands/sql-declare.md)。

### 範例

為查詢宣告游標的範例：

```

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();
```

為預備陳述式宣告游標的範例：

```

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

### 相容性

SQL 標準規定了 `DECLARE`。

### 另請參閱

[OPEN](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-sql-commands/ecpg-sql-open.md), [CLOSE](https://docs.postgresql.tw/19/reference/sql-commands/sql-close.md), [DECLARE](https://docs.postgresql.tw/19/reference/sql-commands/sql-declare.md)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/ecpg-sql-declare.html)（原文版本：18.6；核對日期：2026-10-18）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-sql-commands/ecpg-sql-declare.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
