PostgreSQL SELECT属性来自函数中的Custom类型

时间:2013-08-15 09:51:05

标签: sql function postgresql-9.2

这是我的类型:

CREATE TYPE occupant AS (name text, title title);  

这是我的表:

CREATE TABLE office (id INTEGER DEFAULT NEXTVAL('venue_id_seq'),occupant occupant) INHERITS(venue);

然后这是我的功能:

 CREATE or REPLACE FUNCTION occupantName(id int) RETURNS text AS 
$$
  SELECT occupant.tile + occupant.name FROM office as v WHERE id = v.id;
$$ LANGUAGE SQL;

它给了我这个错误:

ERROR:  missing FROM-clause entry for table "ocupant"
LINE 15:       SELECT ocupant.tile + ocupant.name FROM office as v WH...

1 个答案:

答案 0 :(得分:2)

您必须在占用者周围使用括号来访问复合类型字段:

CREATE or REPLACE FUNCTION occupantName(id int) RETURNS text AS 
$$
  SELECT (occupant).tile || (occupant).name FROM office as v WHERE id = v.id;
$$ LANGUAGE SQL;