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.
Defining parameters within SurrealQL
A parameter can store any value, including the result of a query.
Scope of parameters
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.
Parameters can be defined using SurrealQL as shown above, or can be passed in using the client libraries as request variables.
Redefining and shadowing parameters
Parameters in SurrealQL are immutable. The same parameter can be redefined using a LET statement.
Defining parameters within client libraries
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.
Reserved variable names
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.
For a complete list of reserved parameter names, see this section in the API documentation.