自定义类型

时间:2018-01-16 15:41:17

标签: postgresql types

我有自定义类型:

CREATE TYPE sbjct_with_count AS
(
  sbjct        dp.sbjct_v1,
  num_metrics  bigint
);

使用此自定义类型:

CREATE TYPE sbjct_v1 AS
(
  sbjct_vrsn_id  integer,
  sbjct_id       integer,
  nm             text,
  dsc            text,
  db             text,
  tbl            text,
  pk             text,
  crtd_by        text,
  crtd_ts        timestamp without time zone,
  aprvd_by       text,
  aprvd_ts       timestamp without time zone,
  aprvd_stat     boolean,
  aprvd_del      boolean
);

我有一个查询,我想返回第一个类型的集合(sbjct_with_count)

RETURN QUERY SELECT 
(s.sbjct_vrsn_id,
s.sbjct_id,
s.nm,
s.dsc,
s.db,
s.tbl,
s.pk,
s.crtd_by,
s.crtd_ts,
s.aprvd_by,
s.aprvd_ts,
s.aprvd_stat,
s.aprvd_del) as sbjct,
(SELECT COUNT(*) ... subquery)
FROM dp.sbjct s
... rest of query

如何构造查询的返回部分以适应类型?我收到错误structure of query does not match function result type

1 个答案:

答案 0 :(得分:0)

你应该使用类型转换:

SELECT CAST(
          ROW(
             CAST(ROW(..., ...) AS dp.sbjct_v1),
             ...
          ) AS sbjct_with_count
       )
FROM ...