Skip to main content

Snippets

Snippets are reusable pieces of SQL saved at the group level. A query of the group refers to a snippet by its alias, so common patterns — a standard join, a set of filters, a boilerplate WITH clause — only have to be written once.

Creating a snippet​

Open Snippets in the sidebar (for the group's owner and editors; the editor has it in its sidebar too) and click Create snippet. Fill in:

  • The Alias, the name queries use to refer to it. Use letters, digits and underscores: a reference to an alias with a - or a space is never expanded. An alias is unique in the group ("The given alias already exists!").
  • The Snippet: the SQL body that replaces the reference.

Then click Create.

The snippet is stored on the group and becomes available to every member.

Using a snippet​

In the editor, write snippet('alias') where the fragment belongs (the autocomplete offers snippet('')). The reference stays in the query's SQL and is replaced with the snippet's body each time the query runs. The snippets come from the query's own group, whichever group the person running it has open, and on shared links too. When the group has no snippet with that alias, the run stops with the error Snippet: 'alias' not found! (users who cannot edit the query get a general error message).

$name placeholders in a snippet's body work differently from those in the query's own SQL:

  • they are filled from the URL parameters of the request (its query string);
  • they get no form fields: the form is built from the query's own SQL;
  • a name that the request does not contain becomes NULL;
  • a value that contains Kvery's own query syntax (see How values are inserted) becomes NULL, and when the values form such syntax together with the text of the snippet around them, every value of the snippet becomes NULL.

See Autocomplete and snippets for the editor's other suggestions.

Editing and deleting snippets​

The snippet list shows, for each snippet, the queries that use it. You can edit a snippet's body; its alias cannot be changed. Because queries refer to the snippet, a changed body applies to every query that uses it from its next run.

A snippet can be deleted only while no query uses it: the delete button appears only for unused snippets.

When to use snippets​

  • Standard column lists or joins you repeat across many queries.
  • Boilerplate for Kvery directives (such as error handling or webhooks).
  • Common filter blocks that use form variables.