0

这是示例代码

CREATE TYPE entity AS
(
    record_init boolean,
    b_modified boolean,
    custom_name character varying(8000),
    customer_rec customer,     --- customer is a table 
    lang_pref boolean,
)
do 
$$
declare 
entity_rec entity;
Begin

    entity_rec.record_init := TRUE;
    entity_rec.customer_rec.customer_id := 100;
    raise notice ' Customer Id is %', entity_rec.customer_rec.customer_id;
end;
$$;

从 entity_rec.customer_rec 引用 customer_id 时出错

4

1 回答 1

2

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 schematable. 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
做
于 2021-01-19T12:57:16.417 回答