> 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-statement.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# DECLARE STATEMENT

### DECLARE STATEMENT

DECLARE STATEMENT — 宣告 SQL 陳述式識別字

### 語法

```

EXEC SQL [ AT connection_name ] DECLARE statement_name STATEMENT
```

### 說明

`DECLARE STATEMENT` 會宣告 SQL 陳述式識別字。SQL 陳述式識別字可以與連線相關聯。當動態 SQL 陳述式使用該識別字時，這些陳述式會使用相關聯的連線執行。宣告的命名空間是預編譯單元，不允許對同一個 SQL 陳述式識別字重複宣告。請注意，若預編譯器以 Informix 相容模式執行，且宣告了某個 SQL 陳述式，便不能使用 `database` 作為游標名稱。

### 參數

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

由 `CONNECT` 命令建立的資料庫連線名稱。

`AT` 子句可以省略，但這樣的陳述式沒有意義。

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

SQL 陳述式識別字的名稱，可以是 SQL 識別字或主機變數。

### 注意事項

只有在宣告實際放置於動態陳述式之前（上方）時，此關聯才有效。

### 範例

```

EXEC SQL CONNECT TO postgres AS con1;
EXEC SQL AT con1 DECLARE sql_stmt STATEMENT;
EXEC SQL DECLARE cursor_name CURSOR FOR sql_stmt;
EXEC SQL PREPARE sql_stmt FROM :dyn_string;
EXEC SQL OPEN cursor_name;
EXEC SQL FETCH cursor_name INTO :column1;
EXEC SQL CLOSE cursor_name;
```

### 相容性

`DECLARE STATEMENT` 是 SQL 標準的擴充，但可以在知名的資料庫管理系統中使用。

### 另請參閱

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

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/ecpg-sql-declare-statement.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-statement.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.
