SQLly directives
functionalSmall SQL comments that remember how a result looks: its columns, widths, formats, colors, pivot, and chart.
A directive is an ordinary SQL comment that SQLly understands. To the database it is just a comment, while SQLly uses it to shape or format the result. Because directives live in the query text, they travel with the file and apply again every time you run it.
Every directive shares the same namespace - the word sqlly after the comment marker:
-- sqlly view: pivot
-- sqlly grid: order=Region, Year, Sales; freeze=1; sort=Sales desc
-- sqlly pivot: row:Region; column:Year; value:Sales:sum
-- sqlly chart: kind:bar; label:Region; values:Sales
-- sqlly format: column=Sales; number=2; width=40dp
-- sqlly conditional: column=Sales; bar=#5B9BD5
-- sqlly column: Running Total = !Sales + N(!"Running Total"[1])
You rarely have to type them. Arrange the result the way you like it, then click the lock in front of the result's name: SQLly writes the whole arrangement into the query as one block of directives. See Lock the grid.
The directive families
| Directive | What it does | Example |
|---|---|---|
view: | Opens the result as grid (the default), pivot, or chart. | -- sqlly view: chart |
grid: | The grid's column arrangement: order, hidden and frozen columns, sort, grouping, column filters, and foreign-key labels. grid 2: sets one result set, counting from 1. | -- sqlly grid: hide=Notes; freeze=1; sort=Total desc |
format: | How one result column is displayed, and how wide it is. | -- sqlly format: column=Created; date=%Y-%m-%d; width=40dp |
conditional: | Colors cells by their values for this query: a highlight rule, a color scale, or a data bar. One rule per line. | -- sqlly conditional: column=Status; when=is Failed; text=#B00020; bold |
column: | Adds a column computed with an Excel formula from the result's own columns: !Name is this row, !Name[1] the row before. Excel's functions and operators. | -- sqlly column: Total = !Qty * !Price |
pivot: | Rows, columns, and the aggregated value of a pivot table. result:N picks the result set it belongs to. | -- sqlly pivot: row:Rep; column:Quarter; value:Deals:count |
chart: | Chart type, category label, series, and aggregation. result:N picks the result set it belongs to. | -- sqlly chart: kind:line; label:Month; values:Revenue,Cost |
/* sqlly lock | A block comment SQLly writes when you click the lock on a result. Its -- sqlly lines reproduce the result exactly as it looked. | /* sqlly lock — … … */ |
name | Labels a result set's tab, counting from 1. Handy when a batch returns several. | -- sqlly name 2: Open Orders |
2x2 / result: | Lays several result sets out on one canvas, placing each statement's result in a cell. | -- sqlly result: 1|2 |
layout: | Opens the run's results as stacked, tabbed, or canvas, wherever the file is opened. | -- sqlly layout: stacked |
lines: | Rows allotted to each stacked card; lines 2: sets one result set, counting from 1. | -- sqlly lines 2: 20 |
focus: | Which result set is in view when results arrive: its tab is selected in Tabbed, and its card is scrolled into view in Stacked. Counts from 1. | -- sqlly focus: 2 |
param: | Declares a parameter. A strip above the results lets you type its value before each run. | -- sqlly param: @CustomerId int |
set | Defines a text variable that replaces every ${name} before the query runs. | -- sqlly set schema = dbo |
param: and set are the two that change what the database receives. SQLly substitutes their values into the SQL before sending it, outside strings and comments. A parameter's value goes in as an escaped literal of its declared type (text, int, decimal, bool, date, or datetime). A set value goes in verbatim. Computed-column formulas read both as @name.
Where a directive must go
- On a line of its own. A directive after SQL code on the same line is ignored.
nameis the one exception and works as a trailing comment. view:and the canvas size go on the first line. They are only read on the first non-empty line of the query. In a lock block at the top, the block's first directive line counts as that first line.- Spacing is forgiving for most families:
--sqlly view: pivotand-- SQLLY view: pivotboth work. format:is strict. It must be spelled exactly-- sqlly format:, with a space after--.- Inside a block comment, a directive is switched off, which is a handy way to disable one for a while. The one exception is the
/* sqlly lockblock: its lines work exactly as if each one were written on its own line.
-- sqlly view: pivot -- sqlly pivot: row:Region; column:Year; value:Sales:sum -- sqlly format: column=Sales; number=2 SELECT Region, Year, Sales FROM sales;
sqlly namespace but is misspelled, placed where it cannot apply, or given an unknown value, SQLly underlines it in the editor and says why rather than silently ignoring it. For example, -- sqlly view: table gets Unknown view — expected -- sqlly view: grid, -- sqlly view: pivot, or -- sqlly view: chart.Help in the app
- Hover any
-- sqllycomment for a short description, with a link to the full list of options. - Help › Formatting Directives… (also in the command palette) opens a reference window with every family, its parameters, and copy-ready examples.
- Let SQLly write it. Click the lock in front of a result's name to write the whole result as directives, or right-click a result column header and choose Format › Set Formatting Override… to have one
format:directive written for you.
In this section
- Grid
- Write the column order, hidden and frozen columns, sort, grouping, and filters into the query.
- Formatting in progress
- Give each column the presentation and width it deserves, automatically whenever the query runs.
- Conditional
- Color cells by their values for one query: highlight rules, color scales, and data bars.
- Computed columns
- Add columns written as Excel formulas: running totals, margins, changes from the row before, and dates.
- Pivot
- Turn a familiar result set into a cross-tab with a short, portable comment.
- Chart
- Tell the Chart tab what story to draw before the result even arrives.
- Lock the grid
- One click writes the result exactly as it looks now into the query, so it comes back the same on every run.