Index: doc/src/sgml/ref/select.sgml =================================================================== RCS file: /opt/src/cvs/pgsql/doc/src/sgml/ref/select.sgml,v retrieving revision 1.54 diff -c -r1.54 select.sgml *** doc/src/sgml/ref/select.sgml 23 Apr 2002 02:07:16 -0000 1.54 --- doc/src/sgml/ref/select.sgml 29 Jul 2002 04:16:51 -0000 *************** *** 40,45 **** --- 40,51 ---- ( select ) [ AS ] alias [ ( column_alias_list ) ] | + table_function_name ( [ argtype [, ...] ] ) + [ AS ] alias [ ( column_alias_list | column_definition_list ) ] + | + table_function_name ( [ argtype [, ...] ] ) + AS ( column_definition_list ) + | from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column_list ) ] *************** *** 82,88 **** from_item ! A table reference, sub-SELECT, or JOIN clause. See below for details. --- 88,94 ---- from_item ! A table reference, sub-SELECT, table function, or JOIN clause. See below for details. *************** *** 156,161 **** --- 162,184 ---- + + + table function + + + A table function can appear in the FROM clause. This acts as though + its output were created as a temporary table for the duration of + this single SELECT command. An alias may also be used. If an alias is + written, a column alias list can also be written to provide substitute names + for one or more columns of the table function. If the table function has been + defined as returning the RECORD data type, an alias, or the keyword AS, must + also be present, followed by a column definition list in the form + ( column_name data_type [, ... ] ). + The column definition list must match the actual number and types returned by the function. + + + join_type *************** *** 381,386 **** --- 404,422 ---- + A FROM item can be a table function (i.e. a function that returns + multiple rows and columns). When a table function is created, it may + be defined to return a named scalar or composite data type (an existing + scalar data type, or a table or view name), or it may be defined to return + a RECORD data type. When a table function is defined to return RECORD, it + must be followed in the FROM clause by an alias, or the keyword AS alone, + and then by a parenthesized list of column names and types. This provides + a query-time composite type definition. The FROM clause composite type + must match the actual composite type returned from the function or an + ERROR will be generated. + + + Finally, a FROM item can be a JOIN clause, which combines two simpler FROM items. (Use parentheses if necessary to determine the order of nesting.) *************** *** 925,930 **** --- 961,1003 ---- Warren Beatty Westward Woody Allen + + + + + This example shows how to use a table function, both with and without + a column definition list. + + + distributors: + did | name + -----+-------------- + 108 | Westward + 111 | Walt Disney + 112 | Warner Bros. + ... + + CREATE FUNCTION distributors(int) + RETURNS SETOF distributors AS ' + SELECT * FROM distributors WHERE did = $1; + ' LANGUAGE SQL; + + SELECT * FROM distributors(111); + did | name + -----+------------- + 111 | Walt Disney + (1 row) + + CREATE FUNCTION distributors_2(int) + RETURNS SETOF RECORD AS ' + SELECT * FROM distributors WHERE did = $1; + ' LANGUAGE SQL; + + SELECT * FROM distributors_2(111) AS (f1 int, f2 text); + f1 | f2 + -----+------------- + 111 | Walt Disney + (1 row)