1

假设我在 Postgresql 中创建了一个复合类型:

CREATE TYPE custom_type AS
   (x integer
    y integer);

我需要在函数中将它用作数组:

...
DECLARE
customVar custom_type[];
BEGIN
....

我的问题是:如何访问 custom_type 的特定组件?

例如,我想(重新)为 custom_type 数组中的第三个元素分配“x”......

4

2 回答 2

2

鉴于:

SELECT ARRAY[(1,2),(3,4)]::custom_type[];

使用数组下标,然后按名称引用字段。

regress=> SELECT (ARRAY[(1,2),(3,4)]::custom_type[])[1].x;
 x 
---
 1
(1 row)
于 2012-11-15T11:31:17.320 回答
2
postgres=> 创建类型 pt 为 (x int, y int);
创建类型

postgres=> 创建或替换函数 fx()
返回 void 作为 $$
声明一个 pt[] = ARRAY[(10,20),(30,40)]; 声明 xx pt;
开始
  for i in array_lower(a, 1) .. array_upper(a,1)
  环形
    xx = a[i]; xx.x := xx.x + 1; a[i] := xx; 提高通知 '%', a[i].x;
  结束循环;
结尾;
$$ 语言 plpgsql;
创建函数
postgres=> 选择 fx();
通知:11
通知:31
 外汇
────

(1 行)

Significant limit for target of assign statement is possibility to refer only one level nested properties. This limit can be bypassed by auxiliary variables - it is not too friendly - and internal implementation is too simple, but it is fast and enough for typical stored procedure usage although it is not strong in comparison with generic programming languages.

于 2012-11-16T18:03:55.053 回答