DEFINE PARAM statement
The DEFINE PARAM statement allows you to define global (database-wide) parameters that are available to every client.
Requirements
You must be authenticated as a root owner or editor, namespace owner or editor, or database owner or editor before you can use the
DEFINE PARAMstatement.You must select your namespace and database before you can use the
DEFINE PARAMstatement.
Statement syntax
Example usage
Below shows how you can create a parameter using the DEFINE PARAM statement.
Then, simply use the global parameter like you would with any variable.
Using IF NOT EXISTS clause
The IF NOT EXISTS clause can be used to define a param only if it does not already exist. You should use the IF NOT EXISTS clause when defining a param in SurrealDB if you want to ensure that the param is only created if it does not already exist. If the param already exists, the DEFINE PARAM statement will return an error.
It's particularly useful when you want to safely attempt to define a param without manually checking its existence first.
On the other hand, you should not use the IF NOT EXISTS clause when you want to ensure that the param definition is updated regardless of whether it already exists. In such cases, you might prefer using the OVERWRITE clause, which allows you to define a param and overwrite an existing one if it already exists, ensuring that the latest version of the param definition is always in use
Using OVERWRITE clause
The OVERWRITE clause can be used to define a param and overwrite an existing one if it already exists. You should use the OVERWRITE clause when you want to modify an existing param definition. If the param already exists, the DEFINE PARAM statement will overwrite the existing param definition with the new one.