RETURN statement
The RETURN statement can be used to return an implicit value or the result of a query, and to set the return value for a transaction, block, or function.
Statement syntax
SurrealQL Syntax
RETURN @value
Example usage
Basic usage
RETURN is always followed by a value. As every data type in SurrealDB is a type of value, the RETURN statement can return anything from simple values to the result of queries.
RETURN 123;
RETURN "I am a string!";
RETURN {
prop: "value"
};
RETURN SELECT * FROM person;
RETURN (CREATE person).id;
Values on their own are treated as if they have an implicit RETURN in front. As such, the following queries return the same output as in the previous example.
123;
"I am a string!";
{
prop: "value"
};
SELECT * FROM person;
(CREATE person).id;
Transaction return value
RETURN statements can set the result of any transaction. This includes transactions, blocks and functions.
Transaction return value
BEGIN TRANSACTION;
LET $firstname = "John";
LET $lastname = "Doe";
LET $person = CREATE ONLY person CONTENT {
firstname: $firstname,
lastname: $lastname,
};
RETURN $person.id;
COMMIT TRANSACTION;
Return breaks execution
Available since: v2.0.0
Unlike RETURN in SurrealDB 1.x, RETURN now breaks execution of statements, functions and transactions.
Function return value
DEFINE FUNCTION fn::person::create($firstname: string, $lastname: string) {
LET $person = CREATE person CONTENT {
firstname: $firstname,
lastname: $lastname,
};
RETURN $person.id;
CREATE person SET firstname = "Stephen", lastname = "Strange";
};
fn::person::create("Thanos", "Johnson");
SELECT * FROM person;
Functions
DEFINE FUNCTION fn::round::up($num: number) {
IF $num % 2 == 0 {
RETURN $num;
};
RETURN $num + 1;
};
Transactions
BEGIN;
RETURN 1;
CREATE a;
RETURN 2;
COMMIT;
Lastly, if not executed inside a transaction or function, RETURN will break execution until the most top-level statement it is executed in. RETURN will not prevent top level statements from being executed, nor will it adjust their output.
Statements
LET $id = 123;
LET $id = {
IF $id {
RETURN type::record('table', $id);
};
RETURN table:rand();
};
$id;