Mega Code Archive

 
Categories / PostgreSQL / Postgre SQL
 

SQL Functions Returning Sets

postgres=# postgres=# CREATE TABLE myTable (id   int, postgres(#                       sid  int, postgres(#                       name text); CREATE TABLE postgres=# postgres=# INSERT INTO myTable VALUES (1, 1, 'Joe'); INSERT 0 1 postgres=# INSERT INTO myTable VALUES (1, 2, 'Ed'); INSERT 0 1 postgres=# INSERT INTO myTable VALUES (2, 1, 'Mary'); INSERT 0 1 postgres=# postgres=# -- SQL Functions Returning Sets postgres=# postgres=# CREATE FUNCTION getData(int) RETURNS SETOF myTable AS $$ postgres$#    SELECT * FROM myTable WHERE id = $1; postgres$# $$ LANGUAGE SQL; CREATE FUNCTION postgres=# postgres=# SELECT * FROM getData(1) AS t1;       REATE  id | sid | name ----+-----+------   1 |   1 | Joe   1 |   2 | Ed (2 rows) postgres=# postgres=# drop function getData(int); DROP FUNCTION postgres=# drop table myTable; DROP TABLE postgres=# postgres=#