Back to top
  Back to overview

Parameters

Parameters can be used like variables to store a value which can then be used in a subsequent query. A parameter can store any value, including the result of a query. Parameters can be defined within the SQL, or can be passed in using the client libraries as request variables.

Defining parameters within SurrealQL

To define a parameter in SurrealQL, use the LET statement. The name of the parameter should begin with a $ character.

-- Define the parameter
LET $name = "tobie";
-- Use the parameter
CREATE person SET name = $name;

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.

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

Next steps

You've now learnt how to use parameters and variables within SurrealQL statements, and from the client libraries. Take a look at the next chapter to get an understanding of the different types of statements that can be run on SurrealDB.