6

Here is my type:

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

And here is my table:

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

and then this is my function:

 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;

It is giving me this error:

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

1 Answer 1

8

You have to use parenthesis around occupant to access composite type fields:

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Just to add to your answer I also had to use TEXTCAT(,) instead of the + sign
or || concatenation operator
@RomanPekar Thanks for your answer. This saved me countless hours of debugging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.