Named cells and references

実装済み

Name a cell and read its result from another with {{name}}: how references are folded into SQL, renamed, and reported when they go in a circle.

A finished notebook's lineage, an edit to the base query that marks only the cells reading it out of date, and the Run menu's Run Stale Cells.

Real recording · typing sped up · Northwind demo data

Open the walkthrough on its own page

Give a cell a name and other cells can use its result as if it were a table: write the name in double curly braces. That is how a notebook grows from one base query into a set of small, readable steps instead of one long script, and it is how SQLly knows which cells depend on which.

-- cell "orders"
SELECT o.OrderID, o.CustomerID, o.ShipCountry, o.Freight
FROM dbo.Orders AS o
WHERE YEAR(o.OrderDate) >= 1997

-- another cell
SELECT ShipCountry, SUM(Freight) AS Freight
FROM {{orders}}
GROUP BY ShipCountry

Naming a cell

Select the cell and type a name in the inspector's 名前 field. A name must start with a letter or underscore and use only letters, digits and underscores, and it must be unique in the notebook. SQL reserved words (in any of the dialects SQLly checks) and the word param are refused: "This word is reserved in SQL; choose another name". Names are not case-sensitive, so {{Orders}} そして {{orders}} are the same cell.

Every cell also has an id (shown in the inspector, such as c_qm4tz7a2kd). {{c_qm4tz7a2kd}} works too, and an id reference never changes when the cell is renamed. Ids are matched exactly, including case. Named cells are listed on the left rail's 変数 page and in the Outline.

How a reference becomes SQL

When both cells run on the same connection and the referenced cell is a single read-only query, SQLly folds it into the cell that reads it as a common table expression and sends one query. Nothing is copied out of the database, and the database can optimize the whole thing. The details differ by engine:

エンジンWhat SQLly writes
PostgreSQL, MySQL and MariaDB, SQLite family, OracleThe referenced query becomes a CTE, kept whole, even when it has its own WITH (these engines allow a nested WITH).
SQL ServerSQL Server doesn't allow a WITH inside a CTE, so the referenced query's own CTEs are lifted into the outer WITH list. If two would end up with the same name, or one would hide a table name the query uses, SQLly refuses with a message instead of guessing; rename one. A trailing ORDER BY なし TOP または OFFSET is dropped (it means nothing inside a CTE), and a referenced query ending in FOR XML, FOR JSON または FOR BROWSE can't be folded.

A cell's own leading WITH (と) WITH RECURSIVE) is merged with the folded ones. References inside strings, comments, quoted identifiers and dollar-quoted bodies are ignored, so '{{orders}}' in a string stays text. Parameters are filled in after the references are folded. Run Export ▸ Convert to SQL Script to see the folded SQL for the whole notebook.

A reference can't be folded, and the cell reports why, when the referenced cell:

  • runs on a different connection, or on the notebook's local data: run the reading cell on Local data instead, which copies the result in first (see Join across connections);
  • is not a query: more than one statement, a statement that changes data, or something other than a SELECT;
  • is not a SQL cell (a transform, an attachment): read it from a local data cell.
Name cells differently from tables. A cell named orders that itself reads FROM orders turns into a CTE that refers to itself when another cell folds it in, and engines refuse that. Call it recent_orders.

References in other cells

  • Markdown cells can show a formula cell's value or a parameter: Average order: {{aov}}, Region: {{param.region}}. References inside code spans and fenced code blocks stay as written.
  • Chart, table, pivot, KPI, profile, test and diff cells read their input through their Input picker rather than {{…}}; the link still shows in lineage and still makes them out of date when the input changes.
  • 数式 cells name other cells directly (sum(orders.Freight)); see Formulas and transforms.
  • パラメーター are written {{param.name}} in Markdown and in SQL, and SQL can also use the :name そして @name placeholders. See Parameters and inputs.

Renaming

Rename a cell in the inspector and every {{old_name}} in other SQL and Markdown cells is rewritten to the new name, and so is every formula that names it (sum(orders.Freight) になります sum(recent_orders.Freight)). References written with the cell's id are left alone (they still point at the same cell). Clearing a cell's name rewrites its references to the id, so nothing breaks. A rename is one undo step, and undoing it puts the old name back in the cells that refer to it too.

The same rewriting happens when you rename a cell through find and replace, and when you paste copied cells: references inside the pasted cells point at the pasted copies, and a pasted name that would clash gets a suffix (orders_2).

Seeing the dependencies

The left rail's Lineage page lists, for every SQL cell, what it reads and what uses it ("Reads: orders · Used by: none"), and the inspector shows Reads from そして Used by for the selected cell, each a link. Visual cells add column-level lineage in their Lineage panel; see where a chart's data came from.

SQLly
Lineage rail and inspector dependencies for a notebook cellLineage rail and inspector dependencies for a notebook cell
The Lineage page and the inspector's Reads from and Used by for the selected cell.

Circular references

If cell A reads B and B reads A, no order can run them. SQLly reports the whole loop and suggests which reference to remove, preferring a reference that reads from further down the notebook:

These cells depend on each other in a loop: by_country → orders → by_country
Remove the reference to by_country from orders

Cells on or behind the loop are blocked until it is broken; everything else still runs. Check Without Running そして sqlly-cli notebook validate report loops and references to cells that don't exist without running anything.