RETURN statements
- Last Updated: November 7, 2022
- 1 minute read
- DataDirect Connectors
- JDBC
- Autonomous REST Connector 6.0
- Documentation
A function can have a single-line body that is comprised of a
RETURN
statement. Alternatively, multiple statements (compound statements) can be specified inside
the BEGIN ATOMIC and END statements. Note that there can
be more than one RETURN statement in the body. The following syntax is used
for a RETURN statement: RETURN <expression>where
- expression
- is the argument that determines the value to be returned.
For example, the following function demonstrates using a RETURN statement in a compound
statement:
// This function returns a time one hour before the event starts
RETURNS TIMESTAMP
BEGIN ATOMIC
DECLARE max_event TIMESTAMP;
SET max_event = SELECT MAX(start_time) FROM events WHERE type = e_type;
RETURN max_event - 1 HOUR;
ENDThe next example returns same results as the prior without using the compound statements body:
CREATE FUNCTION an_hour_before_max (e_type INTEGER)
RETURNS TIMESTAMP
RETURN (SELECT MAX(start_time) FROM events WHERE type = e_type) - 1 HOUR
The following example demonstrates a function that uses the RETURN TABLE
syntax to return a table: CREATE FUNCTION alice_and_friends(ignore INTEGER)
RETURNS TABLE(id INTEGER, name VARCHAR(32))
READS SQL DATA
BEGIN ATOMIC
//This function returns a table value. The value can be used by the caller
//anywhere a TABLE clause would go, like SELECT * FROM alice_and_friends(3);
DECLARE TABLE temptable (id INTEGER, name VARCHAR(32));
INSERT INTO temptable VALUES (1, 'Alice');
INSERT INTO temptable VALUES (2, 'Bob');
INSERT INTO temptable VALUES (3, 'Chuck');
RETURN TABLE(SELECT * FROM temptable WHERE id != ignore);
END;