• English
  • SQL Templates

    Write a template once and run it against different tables or values without copying.

    Quick start

    /*
    @vars
    schema: public
    table:
      - orders
      - order_items
      - payments
    */
    
    SELECT COUNT(*) FROM {{ schema }}.{{ table }}
    1. Run normally (Mod+Enter / toolbar ▶). The backend reads @vars, finds {{ schema }} and {{ table }}, expands each combination, and opens a result tab for each row.
    2. Missing variables — If a placeholder has no value yet, a dialog pops up to fill in one value per line. Check "Save values as @vars comment above SQL" to write the filled values back as a @vars block — next run needs no dialog.
    3. Toolbar {} button — Select SQL, click {} in the toolbar to auto-generate an @vars scaffold for all {{ name }} placeholders found in the selection. Fill in values directly in the editor.

    Examples

    Single variable — batch across tables

    /*
    @vars
    table:
      - customers
      - orders
      - products
    */
    
    SELECT COUNT(*) AS cnt FROM {{ table }}

    → 3 result tabs: count of customers, orders, products.

    Multiple variables — cartesian product

    /*
    @vars
    schema: public
    region:
      - us
      - eu
    */
    
    SELECT region, COUNT(*) AS cnt
    FROM {{ schema }}.{{ table_prefix }}_{{ region }}
    GROUP BY region

    With table_prefix set to sales in the dialog, this expands to 2 SQLs (public.sales_us, public.sales_eu), each producing one result tab.

    Date range in WHERE

    /*
    @vars
    table: events
    start_date: '2024-01-01'
    end_date: '2024-12-31'
    */
    
    SELECT date, COUNT(*)
    FROM {{ table }}
    WHERE dt BETWEEN {{ start_date }} AND {{ end_date }}
    GROUP BY date

    Column names in aggregation

    /*
    @vars
    measure: revenue
    segment: country
    */
    
    SELECT {{ segment }}, SUM({{ measure }}) AS total
    FROM sales
    GROUP BY {{ segment }}
    ORDER BY total DESC

    Syntax

    ConstructMeaning
    {{ name }}Variable placeholder (Jinja2 interpolation syntax). Name must be a simple identifier.
    /* @vars*/Leading block comment declaring YAML variable values. Supports scalars (single value) and sequences (multi-value).
    Single valuetable: orders
    Multi valuetable: then - orders / - items (one per line, YAML list)
    Empty scaffoldtable: '' — generated by the {} button when no value exists yet

    How it works

    • Backend (Rust + minijinja): parses the @vars YAML block, renders the Jinja template with UndefinedBehavior::Strict and AutoEscape::None, and performs a cartesian product when several variables have multiple values (max 50 combinations).
    • Frontend (TypeScript): pure helpers (src/lib/sql/macros.ts) for formatting YAML blocks, merging dialog overrides, and building empty scaffolds.
    • No effect on existing SQL without {{ }} placeholders.

    Batch execution

    When a variable has multiple values (YAML list or multi-line in dialog), the system generates one statement per combination and opens a result tab for each. All tabs execute in parallel. A soft limit (20) asks for confirmation; a hard limit (50) rejects.