HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

PostgresSQL: Get single attribute of UDT in SELECT statement

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
udtpostgressqlstatementgetsingleattributeselect

Problem

I created a user-defined type in a PostgreSQL 9.2 database and am trying in the SELECT statement to get only an attribute of the UDT. However, I don't seem to get my code to work.

Creation of type and table:

CREATE TYPE  ttp AS (f1 int, f2 int);
CREATE TABLE tbl (ctyp ttp);


The SELECT statement:

-- does not work
SELECT ctyp.f1 FROM tbl
SELECT ctyp(f1) FROM testtable


Both times I get an error. Is it possible to access a single attribute defined in a UDT in a SELECT?

Solution

Use:
SELECT (ctyp).f1 FROM tbl;

The parentheses are necessary to disambiguate tables from composite types as detailed in the manual on composite types.

Context

StackExchange Database Administrators Q#37164, answer score: 4

Revisions (0)

No revisions yet.