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

# PREPARE

### PREPARE

PREPARE — 準備陳述式以供執行

### 語法

```

PREPARE prepared_name FROM string
```

### 說明

`PREPARE` 會準備一個以字串動態指定的陳述式以供執行。這與直接 SQL 陳述式 [PREPARE](https://docs.postgresql.tw/19/reference/sql-commands/sql-prepare.md) 不同，後者也可以在嵌入式程式中使用。兩種預備陳述式都是以 [EXECUTE](https://docs.postgresql.tw/19/reference/sql-commands/sql-execute.md) 命令執行。

### 參數

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

預備查詢的識別字。

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

包含可預備之 SQL 陳述式（SELECT、INSERT、UPDATE 或 DELETE 其中之一）的字串常值或主機變數。要在執行時提供的參數值，請以問號（`?`）表示。

### 注意事項

一般使用時，*`string`* 是一個主機變數參照，指向包含動態建構之 SQL 陳述式的字串。使用字串常值的情況用處不大；你還不如就寫一個直接 SQL 的 `PREPARE` 陳述式。

若你確實使用字串常值，請記住，你可能想在 SQL 陳述式中加入的任何雙引號，都必須寫成八進位跳脫序列（`\042`），而不是一般 C 慣用的 `\"`。這是因為該字串位於 `EXEC SQL` 區段內，因此 ECPG 詞法分析器會依 SQL 規則而非 C 規則來剖析它。任何內嵌的反斜線稍後會依 C 規則處理；但 `\"` 會被視為常值的結尾，因而立即造成語法錯誤。

### 範例

```

char *stmt = "SELECT * FROM test1 WHERE a = ? AND b = ?";

EXEC SQL ALLOCATE DESCRIPTOR outdesc;
EXEC SQL PREPARE foo FROM :stmt;

EXEC SQL EXECUTE foo USING SQL DESCRIPTOR indesc INTO SQL DESCRIPTOR outdesc;
```

### 相容性

SQL 標準規定了 `PREPARE`。

### 另請參閱

[EXECUTE](https://docs.postgresql.tw/19/reference/sql-commands/sql-execute.md)

***

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