首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用POSTGRESQL的记录和数组

使用POSTGRESQL的记录和数组
EN

Stack Overflow用户
提问于 2021-10-22 10:22:11
回答 1查看 32关注 0票数 1

我尝试重现Oracle复杂类型记录。

显然,我必须创建数据库类型,因为我不能在pgsql声明中创建复杂类型。

代码语言:javascript
复制
create type my_type as (
   .....
);

DO $$
declare
  
  my_cursor cursor  (key varchar)
  for
    select civility, name, firstname, email, telephone
    from my_table
    where sender = key;

  a_type record;
  b_type record;
  c record;
  contact my_type;
  contacts my_type [];
  i numeric;

begin
  select name, road, zcode, town
  into strict a_type 
  from a_table

  i = 0;

  open my_cursor (a_type.name);
  loop
    fetch my_cursor into contact;
    exit when not found;
      contacts[i] = contact;
      i = i + 1;
  end loop;

  select a_type as infos, contacts  into strict b_type;
  select b_type as sender into strict c;

end $$ LANGUAGE 'plpgsql';

这不会导致任何错误。现在,我想要显示我的c变量。在结束之前,我要补充一句:

代码语言:javascript
复制
  raise notice '%', c.sender.infos.name;

在这个上下文中有一个我不能理解的错误:

代码语言:javascript
复制
cross-database references are not implemented: c.sender.infos.name

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-10-22 11:43:01

Postgres不支持记录类型的多个取消引用。以下代码正在运行:

代码语言:javascript
复制
do $$
declare r1 record; r2 record; r3 record; _r1 record; _r2 record;
begin
  select 10 as x, 20 as y into r1;
  select r1 as r1, 30 as z into r2;
  select r2 as r2, 40 as w into r3;
  raise notice '%', to_json(r3);
  _r2 := r3.r2;
  _r1 := _r2.r1;
  raise notice '%', _r1.x;

end;
$$;
NOTICE:  {"r2":{"r1":{"x":10,"y":20},"z":30},"w":40}
NOTICE:  10
DO
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69675234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档