Table-valued functions are functions that can be executed within the FROM clauses
of SELECT statements. These functions return collections that can be converted into
row or column data by using TABLE clauses. Then, the converted data can be processed in SQL statements.
Scenarios
Table-valued functions are suitable for the following scenarios:
- Merging data tables in sessions, such as using an SQL statement to merge data that is stored in different tables.
- Constructing datasets by means of programming, and then passing the datasets to clients in a specific format. You can use table-valued functions to generate constructed datasets without constructing relational tables.
- Improving the performance of parallel queries by using pipelined functions. A table-valued function can serialize the query it belongs to, which can improve the query performance.
Create a table-valued function
- The
RETURNtype of the function must be defined as the collection type. Typically, the collection is a nested table or a variable array. It can also be an associated array.Note The type must be defined at the database level by using theCREATE TYPEstatement. For pipelined functions, the type can also be defined in the package. - All parameters of the function must be in
INmode and compatible with SQL statements. For example, parameters of theRECORDtype are not allowed in the function, because they are not allowed in SQL statements. - The call to the function is embedded in the
TABLEclause.
Examples
Create a table-valued function
- Create a nested table.
CREATE OR REPLACE TYPE polar_strings IS TABLE OF VARCHAR2 (100); - Create a table-valued function that returns the nested table type.
CREATE OR REPLACE FUNCTION get_random_str (count_in IN INTEGER) RETURN polar_strings IS item polar_strings := polar_strings (); BEGIN item.EXTEND (count_in); FOR i IN 1 .. count_in LOOP item (i) := DBMS_RANDOM.string ('u', 10); END LOOP; RETURN item; END; - Invoke the table-valued function in PL/SQL to generate strings.
The following output is returned:DECLARE item polar_strings := get_random_str (5); BEGIN FOR i IN 1 .. item.COUNT LOOP DBMS_OUTPUT.put_line (item (i)); END LOOP; END;CIDKUKWNMV GRSNSGJULU XXCMTMLYUI YWQDIMNEZA BHTWWLCGFN
Invoke the table-valued function.
- Invoke the
get_random_strtable-valued function. The parameter value is set to 5, which specifies to return five random strings.
The following output is returned:SELECT rs.COLUMN_VALUE my_string FROM TABLE (get_random_str (5)) rsmy_string ------------ JAFSOSYOUA VNWSAAAHNA MAEDHVHLIU PRWUJLPKZJ MWZKQZKQJZ (5 rows of records) - Invoke the
get_random_strfunction that contains the explicitly declaredcount_inparameter value. It specifies to return five random strings.
The following output is returned:SELECT COLUMN_VALUE my_string FROM TABLE (get_random_str (count_in => 5))my_string ------------ TRHYTVPPOU DJFDIYAYAF BKJOYQFAJR YCIIBEFSVT OYCUDMUDMX (5 rows of records) - Invoke the
get_random_strfunction that returns five random strings. Then, SUM and AVG functions are invoked to return the total and average lengths of the five random strings.
The following output is returned:SELECT SUM (LENGTH (COLUMN_VALUE)) total_length, AVG (LENGTH (COLUMN_VALUE)) average_length FROM TABLE (get_random_str (5))total_length | average_length --------------+--------------------- 50 | 10.0000000000000000 (1 row of records)