---
url: /process-functions/workbook-workbookfromquery.md
description: Runs a SQL query and returns the results as an Excel workbook, base64-encoded.
---

# workbook.workbookFromQuery

Runs a SQL query on a JDBC datasource and returns the results as an Excel workbook, base64-encoded. The sheet starts with the date it was generated, the datasource and the query, followed by the results with a header row.

```js
workbook.workbookFromQuery(datasource, query, prompts)
```

## Parameters

| Parameter | Type | Description |
|---|---|---|
| `datasource` | `string` | The JDBC datasource's name. |
| `query` | `string` | The SQL query. Use `?` placeholders for values. |
| `prompts` | `string[]` | Optional. Values for the `?` placeholders, in order. |

## Returns

The workbook, base64-encoded, or `null` if it couldn't be created. The reason is written to the process log.

## Examples

```js
const report = workbook.workbookFromQuery("Sales DB", "SELECT * FROM sales");
```

```js
// Email the results of a query
const report = workbook.workbookFromQuery("Sales DB", "SELECT * FROM sales WHERE year = ?", ["2026"]);

new EmailBuilder()
    .to("user@example.com")
    .subject("Sales 2026")
    .body("The report is attached.")
    .attachBase64("sales.xlsx", report)
    .send();
```

## Related

* [ExcelWorkbook](/process-functions/excel-workbook)
* [EmailBuilder](/process-functions/emailbuilder)
