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

# GET DESCRIPTOR

### GET DESCRIPTOR

GET DESCRIPTOR — 從 SQL 描述區取得資訊

### 語法

```

GET DESCRIPTOR descriptor_name :cvariable = descriptor_header_item
GET DESCRIPTOR descriptor_name VALUE column_number :cvariable = descriptor_item [, ... ]
```

### 說明

`GET DESCRIPTOR` 會從 SQL 描述區擷取查詢結果集的相關資訊，並將其存入主機變數。在使用此命令將資訊傳送到主機語言變數之前，描述區通常會先以 `FETCH` 或 `SELECT` 填入資料。

此命令有兩種形式：第一種形式擷取描述區的「標頭」項目，這類項目適用於整個結果集，例如欄位數（原文此處寫作 row count，但下方參數說明指出，目前唯一支援的標頭項目 COUNT 取得的是結果集的欄位數，而不是資料列數）。第二種形式需要以欄位編號作為額外參數，擷取特定欄位的相關資訊，例如欄位名稱與實際的欄位值。

### 參數

#### *`descriptor_name`* [#](#ecpg-sql-get-descriptor-descriptor-name) <a href="#ecpg-sql-get-descriptor-descriptor-name" id="ecpg-sql-get-descriptor-descriptor-name"></a>

描述區名稱。

#### *`descriptor_header_item`* [#](#ecpg-sql-get-descriptor-descriptor-header-item) <a href="#ecpg-sql-get-descriptor-descriptor-header-item" id="ecpg-sql-get-descriptor-descriptor-header-item"></a>

識別要擷取哪一個標頭資訊項目的標記（token）。目前只支援 `COUNT`，用於取得結果集中的欄位數。

#### *`column_number`* [#](#ecpg-sql-get-descriptor-column-number) <a href="#ecpg-sql-get-descriptor-column-number" id="ecpg-sql-get-descriptor-column-number"></a>

要擷取其相關資訊的欄位編號。編號從 1 開始。

#### *`descriptor_item`* [#](#ecpg-sql-get-descriptor-descriptor-item) <a href="#ecpg-sql-get-descriptor-descriptor-item" id="ecpg-sql-get-descriptor-descriptor-item"></a>

識別要擷取欄位的哪一項資訊的標記。支援的項目清單請參閱[第 34.7.1 節](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-descriptors/ecpg-named-descriptors.md#ecpg-named-descriptors)。

#### *`cvariable`* [#](#ecpg-sql-get-descriptor-cvariable) <a href="#ecpg-sql-get-descriptor-cvariable" id="ecpg-sql-get-descriptor-cvariable"></a>

用來接收從描述區擷取之資料的主機變數。

### 範例

擷取結果集欄位數的範例：

```

EXEC SQL GET DESCRIPTOR d :d_count = COUNT;
```

擷取第一個欄位資料長度的範例：

```

EXEC SQL GET DESCRIPTOR d VALUE 1 :d_returned_octet_length = RETURNED_OCTET_LENGTH;
```

以字串形式擷取第二個欄位資料本體的範例：

```

EXEC SQL GET DESCRIPTOR d VALUE 2 :d_data = DATA;
```

以下是完整流程的範例：執行 `SELECT current_database();`，並顯示欄位數、欄位資料長度與欄位資料：

```

int
main(void)
{
EXEC SQL BEGIN DECLARE SECTION;
    int  d_count;
    char d_data[1024];
    int  d_returned_octet_length;
EXEC SQL END DECLARE SECTION;

    EXEC SQL CONNECT TO testdb AS con1 USER testuser;
    EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
    EXEC SQL ALLOCATE DESCRIPTOR d;

    /* Declare, open a cursor, and assign a descriptor to the cursor  */
    EXEC SQL DECLARE cur CURSOR FOR SELECT current_database();
    EXEC SQL OPEN cur;
    EXEC SQL FETCH NEXT FROM cur INTO SQL DESCRIPTOR d;

    /* Get a number of total columns */
    EXEC SQL GET DESCRIPTOR d :d_count = COUNT;
    printf("d_count                 = %d\n", d_count);

    /* Get length of a returned column */
    EXEC SQL GET DESCRIPTOR d VALUE 1 :d_returned_octet_length = RETURNED_OCTET_LENGTH;
    printf("d_returned_octet_length = %d\n", d_returned_octet_length);

    /* Fetch the returned column as a string */
    EXEC SQL GET DESCRIPTOR d VALUE 1 :d_data = DATA;
    printf("d_data                  = %s\n", d_data);

    /* Closing */
    EXEC SQL CLOSE cur;
    EXEC SQL COMMIT;

    EXEC SQL DEALLOCATE DESCRIPTOR d;
    EXEC SQL DISCONNECT ALL;

    return 0;
}
```

執行此範例後，結果會如下所示：

```

d_count                 = 1
d_returned_octet_length = 6
d_data                  = testdb
```

### 相容性

SQL 標準規定了 `GET DESCRIPTOR`。

### 另請參閱

[ALLOCATE DESCRIPTOR](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-sql-commands/ecpg-sql-allocate-descriptor.md), [SET DESCRIPTOR](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-sql-commands/ecpg-sql-set-descriptor.md)

***

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