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

# 34.9.3. ifdef、ifndef、elif、else 與 endif 指令

### 34.9.3. ifdef、ifndef、elif、else 與 endif 指令 [#](#ecpg-ifdef) <a href="#ecpg-ifdef" id="ecpg-ifdef"></a>

你可以使用下列指令有條件地編譯程式碼區段：

#### `EXEC SQL ifdef name;` [#](#ecpg-ifdef-ifdef) <a href="#ecpg-ifdef-ifdef" id="ecpg-ifdef-ifdef"></a>

檢查 *`name`*，若 *`name`* 已透過 `EXEC SQL define name` 定義，則處理後續各行。

#### `EXEC SQL ifndef name;` [#](#ecpg-ifdef-ifndef) <a href="#ecpg-ifdef-ifndef" id="ecpg-ifdef-ifndef"></a>

檢查 *`name`*，若 *`name`* *尚未*透過 `EXEC SQL define name` 定義，則處理後續各行。

#### `EXEC SQL elif name;` [#](#ecpg-ifdef-elif) <a href="#ecpg-ifdef-elif" id="ecpg-ifdef-elif"></a>

在 `EXEC SQL ifdef name` 或 `EXEC SQL ifndef name` 指令之後，開始一個選用的替代區段。`elif` 區段可以出現任意多個。若 *`name`* 已定義，*而且*同一個 `ifdef`/`ifndef`...`endif` 結構中先前沒有任何區段被處理過，則會處理 `elif` 之後的各行。

#### `EXEC SQL else;` [#](#ecpg-ifdef-else) <a href="#ecpg-ifdef-else" id="ecpg-ifdef-else"></a>

在 `EXEC SQL ifdef name` 或 `EXEC SQL ifndef name` 指令之後，開始一個選用的最終替代區段。若同一個 `ifdef`/`ifndef`...`endif` 結構中先前沒有任何區段被處理過，則會處理後續各行。

#### `EXEC SQL endif;` [#](#ecpg-ifdef-endif) <a href="#ecpg-ifdef-endif" id="ecpg-ifdef-endif"></a>

結束一個 `ifdef`/`ifndef`...`endif` 結構。後續各行會照常處理。

`ifdef`/`ifndef`...`endif` 結構可以巢狀使用，最多 127 層。

此範例會在三個 `SET TIMEZONE` 命令中恰好編譯其中一個：

```

EXEC SQL ifdef TZVAR;
EXEC SQL SET TIMEZONE TO TZVAR;
EXEC SQL elif TZNAME;
EXEC SQL SET TIMEZONE TO TZNAME;
EXEC SQL else;
EXEC SQL SET TIMEZONE TO 'GMT';
EXEC SQL endif;
```

[返回 34.9. 前置處理器指令](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-preproc.md#ecpg-preproc)。

***

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