A document report is one row in api_carbone_report plus a Word .docx template uploaded to that row. Rendering is performed outside SQL Server by the dispatcher; the database holds only the definition.
api_carbone_report
| Column | Meaning |
|---|
name | Unique lookup key used in every procedure call |
template_name | Descriptive label only — it does not locate the file |
data_sql | Query producing the JSON payload (see contract below) |
source_card_id | Build the payload from card metadata instead, when data_sql is NULL |
report_type | generic (default) or invoice |
action_sql | Query returning the delivery instruction |
is_active | Set to 0 to disable without deleting |
The template
Upload the .docx to the report record itself — it is an ordinary file attachment, stored like any other upload. The newest non-deleted .docx on the record wins, so re-uploading supersedes the previous template rather than overwriting it. Placeholders use Carbone syntax: {d.customer.name} for a field, {d.lines[i].amount} for a row in an array.
data_sql contract
Must return one row with one column named data, containing a JSON string. @context_id is available as a parameter.
T‑SQL
SELECT data = (
SELECT
invoice = JSON_QUERY((
SELECT [number], [date], [total]
FROM Invoice AS this
WHERE this.id = @context_id
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
)),
lines = JSON_QUERY((
SELECT [description], [amount]
FROM InvoiceLine AS this
WHERE this.invoice_id = @context_id
FOR JSON PATH
))
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
)
Wrap nested sets in JSON_QUERY(...), or SQL Server escapes them and Carbone receives a string where it expects an object. Use WITHOUT_ARRAY_WRAPPER for single objects only — an array needs the wrapper or the template cannot loop it.
action_sql contract
Must return one row describing delivery: action (email or download), value (the recipient), subject, from_email. It may also reference @context_id, so the recipient can be looked up per record.
Inspecting reports
v_api_carbone_reports lists every report with the template actually resolved: has_template, template_file, template_path, and data_source (data_sql, card or none). A report with has_template = 0 or data_source = 'none' cannot render.