• Start

Concepts & Guides

Parameterised queries

Queries can make use of parameters which hold stored values, even those from the output of a previous query.

Parameters are stored values which can then be used in subsequent queries. To define a parameter in SurrealQL, use the LET statement. Parameter names must begin with a $ (dollar sign) character.

-- Define the parameter
LET $suffix = "Morgan Hitchcock";
-- Use the parameter
CREATE person SET name = "Tobie " + $suffix;
-- (Another way to do the same)
CREATE person SET name = string::join(" ", "Jaime", $suffix);

Response

[
{
"id": "person:3vs17lb9eso9m7gd8mml",
"name": "Tobie Morgan Hitchcock"
}
]

[
{
"id": "person:xh4zbns5mgmywe6bo1pi",
"name": "Jaime Morgan Hitchcock"
}
]

A parameter can store any value, including the result of a query.

-- Assuming the CREATE statements from the previous example
LET $founders = SELECT * FROM person;
$founders.{
name,
company
};

Response

[
{
company: 'SurrealDB',
name: 'Jaime Morgan Hitchcock'
},
{
company: 'SurrealDB',
name: 'Tobie Morgan Hitchcock'
}
]

Parameters persist across the current connection, and thus can be reused between different namespaces and databases. In the example below, a created person record assigned to a parameter is reused in a query in a completely different namespace and database.

LET $billy = CREATE ONLY person:billy SET name = "Billy";
-- Fails as `person:billy` already exists
CREATE person CONTENT $billy;

USE NAMESPACE other_namespace;
USE DATABASE other_database;
-- Succeeds as `person:billy` does not yet
-- exist in this namespace and database
CREATE person CONTENT $billy;

Parameters can be defined using SurrealQL as shown above, or can be passed in using the client libraries as request variables.

Parameters in SurrealQL are immutable. The same parameter can be redefined using a LET statement.

LET $my_name = "Alucard";
LET $my_name = "Sypha";
RETURN $my_name;

Output

'Sypha'

SurrealDB's client libraries allow parameters to be passed in as JSON values, which are then converted to SurrealDB data types when the query is run. The following example show a variable being used within a SurrealQL query from the JavaScript library.

let people = await surreal.query("SELECT * FROM article WHERE status INSIDE $status", {
status: ["live", "draft"],
});

SurrealDB automatically predefines certain variables depending on the type of operation being performed. For example, $this and $parent are automatically predefined for subqueries so that the fields of one can be compared to another if necessary. In addition, the predefined variables $access, $auth, $token, and $session are protected variables used to give access to parts of the current database configuration and can never be overwritten.

LET $access = true;
LET $auth = 10;
LET $token = "Mytoken";
LET $session = rand::int(0, 100);

Output

-------- Query 1 --------

"'access' is a protected variable and cannot be set"

-------- Query 2 --------

"'auth' is a protected variable and cannot be set"

-------- Query 3 --------

"'token' is a protected variable and cannot be set"

-------- Query 4 --------

"'session' is a protected variable and cannot be set"

For a complete list of reserved parameter names, see this section in the API documentation.

Was this page helpful?