PLpgSQL 不支持 assign 语句左侧的复杂表达式。您应该将此操作划分为更多操作(此问题已在准备好的 PostgreSQL 14 中修复):
DECLARE
entity_rec entity;
customer_rec customer;
BEGIN
entity_rec.record_init := TRUE;
customer_rec.customer_id := 100;
entity_rec.customer_rec := customer_rec;
raise notice ' Customer Id is %', entity_rec.customer_rec.customer_id;
END;
第二个问题可能是嵌套记录的取消引用。Postgres 默认使用 schema schema。table. name. 当您使用嵌套复合值时,应使用括号:
postgres=# 做 $$
宣布
entity_rec 实体;
customer_rec 客户;
开始
entity_rec.record_init := TRUE;
customer_rec.customer_id := 100;
entity_rec.customer_rec := customer_rec;
提出通知'客户 ID 为 %',entity_rec.customer_rec.customer_id;
结尾;
$$;
错误:缺少表“customer_rec”的 FROM 子句条目
第 1 行:选择 entity_rec.customer_rec.customer_id
^
查询:选择 entity_rec.customer_rec.customer_id
上下文: PL/pgSQL 函数 inline_code_block 第 9 行在 RAISE
正确的:
postgres=# 做 $$
宣布
entity_rec 实体;
customer_rec 客户;
开始
entity_rec.record_init := TRUE;
customer_rec.customer_id := 100;
entity_rec.customer_rec := customer_rec;
提出通知'客户 ID 为 %', (entity_rec).customer_rec.customer_id;
结尾;
$$;
注意:客户 ID 为 100
做