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

# 41.5.5. 取得結果狀態

## 41.5.5. 取得結果狀態 [#](#plpgsql-statements-diagnostics) <a href="#plpgsql-statements-diagnostics" id="plpgsql-statements-diagnostics"></a>

有好幾種方式可以判斷一個指令的效果。第一種方式是使用 `GET DIAGNOSTICS` 指令，其形式為：

```

GET [ CURRENT ] DIAGNOSTICS variable { = | := } item [ , ... ];
```

這個指令可以取得系統的狀態指標。`CURRENT` 是一個無作用的字詞（但另請參閱[第 41.6.8.1 節](https://docs.postgresql.tw/19/server-programming/plpgsql/plpgsql-control-structures/plpgsql-error-trapping.md#plpgsql-exception-diagnostics)中的 `GET STACKED DIAGNOSTICS`）。每一個 *`item`* 都是一個關鍵字，用來標明要指派給指定之 *`variable`*（該變數的資料型別應該要能接收它）的狀態值。目前可用的狀態項目列於[表 41.1](#plpgsql-current-diagnostics-values)。冒號等號（`:=`）可以用來取代 SQL 標準的 `=` 語彙單元。一個例子：

```

GET DIAGNOSTICS integer_var = ROW_COUNT;
```

### 表 41.1. 可用的診斷資訊項目 [#](#plpgsql-current-diagnostics-values) <a href="#plpgsql-current-diagnostics-values" id="plpgsql-current-diagnostics-values"></a>

| 名稱               | 型別       | 說明                                                                                                                                      |
| ---------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `ROW_COUNT`      | `bigint` | 最近一個 SQL 指令所處理的資料列數                                                                                                                     |
| `PG_CONTEXT`     | `text`   | 描述目前呼叫堆疊的文字行（參閱[第 41.6.9 節](https://docs.postgresql.tw/19/server-programming/plpgsql/plpgsql-control-structures.md#plpgsql-call-stack)） |
| `PG_ROUTINE_OID` | `oid`    | 目前函式的 OID                                                                                                                               |

<br>

判斷指令效果的第二種方式，是檢查名為 `FOUND` 的特殊變數，其型別為 `boolean`。在每次 PL/pgSQL 函式呼叫中，`FOUND` 一開始是 false。它會被下列各種陳述式設定：

* `SELECT INTO` 陳述式在有資料列被指派時把 `FOUND` 設為 true，若沒有回傳資料列則設為 false。
* `PERFORM` 陳述式在產生（並捨棄）一筆以上的資料列時把 `FOUND` 設為 true，若沒有產生資料列則設為 false。
* `UPDATE`、`INSERT`、`DELETE` 與 `MERGE` 陳述式在至少有一筆資料列受影響時把 `FOUND` 設為 true，若沒有資料列受影響則設為 false。
* `FETCH` 陳述式在回傳一筆資料列時把 `FOUND` 設為 true，若沒有回傳資料列則設為 false。
* `MOVE` 陳述式在成功重新定位游標時把 `FOUND` 設為 true，否則設為 false。
* `FOR` 或 `FOREACH` 陳述式在迭代一次以上時把 `FOUND` 設為 true，否則設為 false。`FOUND` 是在迴圈離開時才這樣設定；在迴圈的執行過程中，`FOUND` 不會被該迴圈陳述式修改，不過它有可能被迴圈主體內其他陳述式的執行所改變。
* `RETURN QUERY` 與 `RETURN QUERY EXECUTE` 陳述式在查詢至少回傳一筆資料列時把 `FOUND` 設為 true，若沒有回傳資料列則設為 false。

其他的 PL/pgSQL 陳述式不會改變 `FOUND` 的狀態。特別要注意的是，`EXECUTE` 會改變 `GET DIAGNOSTICS` 的輸出，但不會改變 `FOUND`。

`FOUND` 在每個 PL/pgSQL 函式中都是一個區域變數；對它所做的任何變更都只影響目前的函式。

[返回 41.5. 基本陳述式](https://docs.postgresql.tw/19/server-programming/plpgsql/plpgsql-statements.md#plpgsql-statements)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/plpgsql-statements.html)（原文版本：18.6；核對日期：2026-09-13）


---

# 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/server-programming/plpgsql/plpgsql-statements/plpgsql-statements-diagnostics.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.
