Writing SQL
In the editor you write standard SQL for your chosen database — MySQL/MariaDB, PostgreSQL, or MSSQL. Kvery sends it to the database largely as-is (see How Kvery runs the text), while adding a few conveniences for parameters, error handling, and result formatting.
Read vs. write queries
Kvery looks at the words of your SQL to decide how the query behaves:
- Write queries contain
INSERT,UPDATE,DELETE,REPLACE,ALTERorCREATEfollowed by a space anywhere in the text, in any letter case (also in a comment or a string). They take their values from posted data: the run form posts them, and Public Query API callers send them withPOST,PUT,PATCHorDELETE. - Read queries are all other queries (
SELECT,EXEC,WITH …). They return rows, and API callers can pass their values with any method.
This drives the run UI and the Public Query API. You do not configure it manually.
Parameters (form variables)
Use a $-prefixed name to introduce a parameter:
SELECT *
FROM invoices
WHERE customer_id = '$customer_id'
AND issued_at >= '$from_date';
Kvery renders a form field for each $-prefixed parameter and fills it from the
request when the query runs. Apostrophes in the values are escaped, so put text
and dates in quotes. Full details — field types, defaults, validation, and special
inputs (checkbox/radio/select) — are in
Form variables.
Internal variables
Kvery also exposes internal variables you can reference in SQL, such as contextual values that Kvery fills in at run time. See Internal variables.
Multiple statements and CTEs
You can use CTEs (WITH ...) in read queries. When combining statements, keep in
mind the read/write classification looks at the whole text: one write keyword
anywhere makes the query a write query. When the statements are written in blocks
({% … %}) and the last block starts with SELECT or EXEC, the query runs like
a read query and returns the rows of that block.
How Kvery runs the text
Before the query runs, Kvery prepares its text:
- The text runs as one line: line breaks are removed, and every other run of white space becomes a single space — inside quoted text and in the filled-in values too. When you save a query, Kvery puts a space before every line break that follows a word, so the words of two lines stay apart; a line break inside a value is removed without a space.
- Block comments (
/* … */, also/*! … */and/*+ … */) are removed. A line comment (--, on MySQL and MariaDB also#) would hide the rest of its statement up to the next;, so write comments as/* … */. - The text is split into statements at every
;outside single-quoted strings (also at a;inside a comment), and the statements run one after the other; the result of the last statement is the result of the query. - Statement blocks group the statements: when the text has
{% … %}blocks, only the text inside the blocks runs, block by block. When the text contains[%anywhere,[% … %]blocks are used instead of{% … %}.
Keeping queries fast
- Add
LIMIT(or the database equivalent) while developing. - Index the columns you filter and join on.
- Remember the query timeout (100 seconds by default).
Next
- Autocomplete and snippets
- Directives — error handling, HTTP codes, and webhooks.
- Column modifiers — change how columns are displayed.