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

# 34.4.4. 型別對應

### 34.4.4. 型別對應 [#](#ecpg-variables-type-mapping) <a href="#ecpg-variables-type-mapping" id="ecpg-variables-type-mapping"></a>

當 ECPG 應用程式在 PostgreSQL 伺服器與 C 應用程式之間交換值時，例如從伺服器取得查詢結果，或執行帶有輸入參數的 SQL 陳述式時，這些值需要在 PostgreSQL 資料型別與主機語言變數型別（具體而言就是 C 語言資料型別）之間轉換。ECPG 的重點之一，就是在大多數情況下會自動處理這件事。

就這方面而言，資料型別分為兩類：一些簡單的 PostgreSQL 資料型別，例如 `integer` 與 `text`，應用程式可以直接讀寫。其他 PostgreSQL 資料型別，例如 `timestamp` 與 `numeric`，則只能透過特殊的函式庫函式存取；請參閱[第 34.4.4.2 節](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-variables/ecpg-variables-type-mapping/ecpg-variables-special-types.md#ecpg-special-types)。

[表 34.1](#ecpg-datatype-hostvars-table) 列出哪些 PostgreSQL 資料型別對應到哪些 C 資料型別。當你想要傳送或接收某個 PostgreSQL 資料型別的值時，應該在宣告區段中宣告對應 C 資料型別的 C 變數。

#### 表 34.1. PostgreSQL 資料型別與 C 變數型別之間的對應 [#](#ecpg-datatype-hostvars-table) <a href="#ecpg-datatype-hostvars-table" id="ecpg-datatype-hostvars-table"></a>

<table><thead><tr><th>PostgreSQL 資料型別</th><th>主機變數型別</th></tr></thead><tbody><tr><td><code>smallint</code></td><td><code>short</code></td></tr><tr><td><code>integer</code></td><td><code>int</code></td></tr><tr><td><code>bigint</code></td><td><code>long long int</code></td></tr><tr><td><code>decimal</code></td><td><code>decimal</code><a href="#ftn.ECPG-DATATYPE-TABLE-FN"><sup>[a]</sup></a></td></tr><tr><td><code>numeric</code></td><td><code>numeric</code><a href="#ftn.ECPG-DATATYPE-TABLE-FN"><sup>[a]</sup></a></td></tr><tr><td><code>real</code></td><td><code>float</code></td></tr><tr><td><code>double precision</code></td><td><code>double</code></td></tr><tr><td><code>smallserial</code></td><td><code>short</code></td></tr><tr><td><code>serial</code></td><td><code>int</code></td></tr><tr><td><code>bigserial</code></td><td><code>long long int</code></td></tr><tr><td><code>oid</code></td><td><code>unsigned int</code></td></tr><tr><td><code>character(</code><em><code>n</code></em><code>)</code>, <code>varchar(</code><em><code>n</code></em><code>)</code>, <code>text</code></td><td><code>char[</code><em><code>n</code></em><code>+1]</code>, <code>VARCHAR[</code><em><code>n</code></em><code>+1]</code></td></tr><tr><td><code>name</code></td><td><code>char[NAMEDATALEN]</code></td></tr><tr><td><code>timestamp</code></td><td><code>timestamp</code><a href="#ftn.ECPG-DATATYPE-TABLE-FN"><sup>[a]</sup></a></td></tr><tr><td><code>interval</code></td><td><code>interval</code><a href="#ftn.ECPG-DATATYPE-TABLE-FN"><sup>[a]</sup></a></td></tr><tr><td><code>date</code></td><td><code>date</code><a href="#ftn.ECPG-DATATYPE-TABLE-FN"><sup>[a]</sup></a></td></tr><tr><td><code>boolean</code></td><td><code>bool</code><a href="#ftn.id-1.7.5.10.7.5.2.2.17.2.2"><sup>[b]</sup></a></td></tr><tr><td><code>bytea</code></td><td><code>char *</code>, <code>bytea[</code><em><code>n</code></em><code>]</code></td></tr><tr><td colspan="2"><p><a href="#ECPG-DATATYPE-TABLE-FN"><sup>[a]</sup> </a>此型別只能透過特殊的函式庫函式存取；請參閱<a href="https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-variables/ecpg-variables-type-mapping/ecpg-variables-special-types.md#ecpg-special-types">第 34.4.4.2 節</a>。</p><p><a href="#id-1.7.5.10.7.5.2.2.17.2.2"><sup>[b]</sup> </a>若非原生型別，則宣告於 <code>ecpglib.h</code></p></td></tr></tbody></table>

<br>

#### 34.4.4.1. 處理字元字串 [#](#ecpg-char) <a href="#ecpg-char" id="ecpg-char"></a>

若要處理 SQL 字元字串資料型別，例如 `varchar` 與 `text`，宣告主機變數有兩種可行的方式。

一種是使用 `char[]`，也就是 `char` 的陣列，這是 C 語言中處理字元資料最常見的方式。

```

EXEC SQL BEGIN DECLARE SECTION;
    char str[50];
EXEC SQL END DECLARE SECTION;
```

請注意，你必須自行處理長度。如果你把這個主機變數用作某個查詢的目標變數，而該查詢傳回超過 49 個字元的字串，就會發生緩衝區溢位。

另一種方式是使用 `VARCHAR` 型別，這是 ECPG 提供的特殊型別。`VARCHAR` 型別陣列的定義，會針對每個變數轉換成一個具名的 `struct`。像這樣的宣告：

```

VARCHAR var[180];
```

會被轉換成：

```

struct varchar_var { int len; char arr[180]; } var;
```

成員 `arr` 存放字串，包含結尾的零位元組。因此，若要在 `VARCHAR` 主機變數中存放字串，宣告該主機變數時，長度必須把零位元組結尾符號也算進去。成員 `len` 存放 `arr` 中所儲存字串的長度，不含結尾的零位元組。當主機變數用作查詢的輸入時，若 `strlen(arr)` 與 `len` 不同，會使用較短的那一個。

`VARCHAR` 可以全部大寫或全部小寫，但不能大小寫混用。

`char` 與 `VARCHAR` 主機變數也可以存放其他 SQL 型別的值，這些值會以字串形式儲存。

#### 34.4.4.2. 存取特殊資料型別 [#](#ecpg-special-types) <a href="#ecpg-special-types" id="ecpg-special-types"></a>

本節內容請參閱[34.4.4.2. 存取特殊資料型別](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-variables/ecpg-variables-type-mapping/ecpg-variables-special-types.md#ecpg-special-types)。

#### 34.4.4.3. 非基本型別的主機變數 [#](#ecpg-variables-nonprimitive-c) <a href="#ecpg-variables-nonprimitive-c" id="ecpg-variables-nonprimitive-c"></a>

本節內容請參閱[34.4.4.3. 非基本型別的主機變數](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-variables/ecpg-variables-type-mapping/ecpg-variables-nonprimitive-c.md#ecpg-variables-nonprimitive-c)。

[返回 34.4. 使用主機變數](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-variables.md#ecpg-variables)

***

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