Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
:sectnums:
:sectnumlevels: 5

= FOR 循环隐式记录实现

== 绑定循环变量

`src/pl/plisql/src/pl_gram.y` 的查询循环规则可为未声明的循环目标创建记录。变量名注册在该循环的 PL/iSQL 命名空间中,因此循环体能够解析 `item.invoice_id` 等引用。

== 逐行赋值

运行时,查询循环先将结果行赋给记录,再执行循环体。查询的元组描述符提供字段名称和类型,表达式的字段名由 SELECT 别名决定。

可运行的示例见 xref:master/oracle_compatibility/compat_for_loop_implicit_record.adoc[FOR 循环隐式记录]。
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
:sectnums:
:sectnumlevels: 5

= FOR 循环隐式记录

== 遍历查询结果

在 PL/iSQL 中,查询 `FOR` 循环可以隐式声明一个记录变量。使用查询的列名或别名读取每行的字段。对于计算列,请在 SELECT 列表中指定别名,方便在循环体内引用。

[source,sql]
----
BEGIN
FOR item IN (
SELECT 1 AS invoice_id, 'open' AS label FROM dual
UNION ALL
SELECT 2, 'paid' FROM dual
ORDER BY invoice_id
) LOOP
RAISE NOTICE '% %', item.invoice_id, item.label;
END LOOP;
END;
/
----

代码块先输出 `1 open`,再输出 `2 paid`。循环引入的 `item` 在循环体内使用。示例通过 `ORDER BY` 明确遍历顺序。

实现细节见 xref:master/compatibility_features_design/for_loop_implicit_record_impl.adoc[隐式记录实现]。
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
:sectnums:
:sectnumlevels: 5

= FOR Loop Implicit Record Implementation

== Bind the loop variable

The query-loop rule in `src/pl/plisql/src/pl_gram.y` can introduce a record datum for an undeclared loop target. The name belongs to the loop's PL/iSQL namespace, which lets the body resolve references such as `item.invoice_id`.

== Assign each row

At execution, the query loop assigns the result row to that record before executing the body. The query's tuple descriptor supplies the record's field names and types, so SELECT aliases determine the field names for expressions.

For a runnable example, see xref:master/oracle_compatibility/compat_for_loop_implicit_record.adoc[FOR loop implicit records].
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
:sectnums:
:sectnumlevels: 5

= FOR Loop Implicit Records

== Iterate over query rows

In PL/iSQL, a query `FOR` loop can introduce its own record variable. Use the query's column names or aliases to access the fields of each row. For computed columns, supply a name in the SELECT list so the field is easy to reference.

[source,sql]
----
BEGIN
FOR item IN (
SELECT 1 AS invoice_id, 'open' AS label FROM dual
UNION ALL
SELECT 2, 'paid' FROM dual
ORDER BY invoice_id
) LOOP
RAISE NOTICE '% %', item.invoice_id, item.label;
END LOOP;
END;
/
----

The block prints `1 open` followed by `2 paid`. The loop introduces `item` for use in its body. The example's `ORDER BY` makes the iteration order explicit.

See xref:master/compatibility_features_design/for_loop_implicit_record_impl.adoc[implicit record implementation].
Loading