Mega Code Archive

 
Categories / PostgreSQL / Select Query
 

Cast row function

postgres=# postgres=# CREATE FUNCTION getf1(mytable) RETURNS int postgres-# AS 'SELECT $1.f1' postgres-# LANGUAGE SQL; CREATE FUNCTION postgres=# postgres=# CREATE TYPE myrowtype AS (f1 int, f2 text, f3 numeric); CREATE TYPE postgres=# postgres=# CREATE FUNCTION getf1(myrowtype) RETURNS int postgres-# AS 'SELECT $1.f1' postgres-# LANGUAGE SQL; CREATE FUNCTION postgres=# postgres=# SELECT getf1(ROW(1,2.5,'this is a test')::mytable);  getf1 -------      1 (1 row) postgres=# postgres=# SELECT getf1(CAST(ROW(11,'this is a test',2.5) AS myrowtype));  getf1 -------     11 (1 row) postgres=# postgres=# drop function getf1(myrowtype); DROP FUNCTION postgres=# drop type myrowtype; DROP TYPE postgres=# drop table mytable cascade; NOTICE:  drop cascades to function getf1(mytable) DROP TABLE postgres=# postgres=#