Prepared Statements

Cypher can execute read-only queries within prepared statements. When using parameters with stored procedures, a SQL parameter must be passed to the Cypher function call. For details, see Cypher Query Format.

Cypher Parameter Syntax

A Cypher parameter has the form $ followed by an identifier. Unlike PostgreSQL parameters, Cypher parameters must begin with a letter and may be followed by any alphanumeric string.

Example: $parameter_name

Preparing a Prepared Statement

Preparing a Cypher statement is an extension of the PostgreSQL stored procedure system. Use the PREPARE command to define a query containing a Cypher function call. Do not use PostgreSQL-style positional parameters (e.g., $1) inside the Cypher query string. Instead, use Cypher parameters (e.g., $name) in the Cypher query, and pass the corresponding PostgreSQL parameter as the third argument to the cypher() function.

PREPARE cypher_stored_procedure(agtype) AS
SELECT *
FROM cypher('expr', $$
    MATCH (v:Person)
    WHERE v.name = $name  // Cypher parameter
    RETURN v
$$, $1)  // SQL parameter must be passed as the third argument to cypher()
AS (v agtype);

Executing a Prepared Statement

When executing the prepared statement, supply an agtype map containing the parameter values at the position of the SQL parameter in the cypher() call. The value must be a valid agtype map; otherwise, an error is raised. Parameter names in the map omit the leading $.

EXECUTE cypher_stored_procedure('{"name": "Tobias"}');