DRAFTThis page is not published. Only visible in development mode.

Variables

A variable binds a value to a name with let and is referenced as $name anywhere in the request - in expressions, in pipelines, in later statements. Variables are what turn a one-shot query into a script: compute a value once, then filter, branch, or write with it, all without leaving RQL.

Bind once, use anywhere

A let statement ends with a semicolon like any other statement, and the binding is visible to everything after it in the same request. Here the threshold is named once instead of being repeated through the pipeline. Run the snippets on this page in order:

$ Bind a Value Once, Use It in the Pipeline
$ ctrl+enter to run

Reassignment

After a let, plain assignment ($x = ...) replaces the value. This is what loop bodies use to accumulate - see for and its siblings:

$ Reassign After Declaring
$ ctrl+enter to run

Assignment does not declare. Writing to a name that was never bound with let is an error, so a typo in a variable name fails loudly instead of silently creating a second variable:

$ Assignment Without let Is an Error
$ ctrl+enter to run

Frame variables

A variable can hold a whole query result, not just a scalar. Binding let $items = from cf_var::items; captures the frame; a later from $items reads it like any other source, with the usual transforms applied after it:

$ Frame Variables Hold Whole Results
$ ctrl+enter to run

When the frame has a single row, a field access reads one value out of it. This is the idiom for configuration tables - fetch the row once, then use its fields as scalars:

$ Read a Field from a Single-Row Frame
$ ctrl+enter to run
Capture with a bare from
Capture frame variables with a bare from source and apply transforms after from $var. In the current build, putting transforms inside the let itself (let $x = from t filter { ... }) is not supported.

Scoping

Every block ({ ... }) is a scope. A let inside an if or loop body declares a new variable that shadows the outer one until the block ends - it does not overwrite it. To change an outer variable from inside a block, assign ($x = ...) instead of re-declaring:

$ Blocks Create Scopes
$ ctrl+enter to run

Closures interact with scopes too: they capture the variables visible where they are defined.

Variables live for one request
A variable exists from its let to the end of the request - there is no session state to clean up, and nothing leaks between requests. For the rest of the scripting toolkit, start at the Control Flow overview.