---
title: Query Datasource
description: Execute a query on a connected data source and retrieve results as in-memory data or exported file.
category: Infoveave Activity
tags: [query, datasource, sql, export, file, database]
---

# Query Datasource

## Description

The **Query DataSource** activity executes a structured query (e.g., SQL) against a configured data source such as a relational database, data warehouse, or cloud storage engine. You can retrieve the results directly as tabular output or optionally save them to a file using configurable export settings.

This activity is especially useful for dynamically pulling data into a workflow, generating reports, or integrating external data systems. When saving results to a file, it also supports advanced formatting using templates, encryption, and partitioning logic.

Use this activity to:

- Query databases and bring live data into workflows
- Export query results as Excel or CSV files
- Apply formatting using templates and control sheet/row structure
- Secure output files with encryption and filename customization

> **Use case**:
> A data analyst can use this activity to run a sales report query on a PostgreSQL database. The output can be formatted using a template Excel file, encrypted using a secret key, and saved with a filename pattern that includes a timestamp.

## Input

| Type     | Description                                                                   |
| -------- | ----------------------------------------------------------------------------- |
| **Data** | Optional incoming data used to influence the query or filenames (if dynamic). |

## Output

| Output Type | Format        | Description                                                       |
| ----------- | ------------- | ----------------------------------------------------------------- |
| **Data**    | Table or File | Query results, either in-memory (as table) or written to file(s). |

## Configuration Fields

| Field Name              | Description                                                                                                                                          |
| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Datasource Id**       | Select the connection representing the data source (e.g., MySQL, SQL Server, Snowflake) where the query will run.                                    |
| **Query**               | The query string (SQL or supported query language) to be executed on the selected data source. Supports variables if dynamic queries are needed.     |
| **Save As File**        | Enable this option to save the query result to a file instead of returning it as in-memory table data.                                               |
| **Secret Key**          | Required only when "Save As File" is enabled. This encryption key secures the generated file's content.                                              |
| **Sheet Name**          | Applicable for Excel files. Specifies the sheet name where query results will be written.                                                            |
| **File Pattern**        | A pattern to define the file name dynamically, such as using a timestamp or input variable (e.g., `SalesReport_{Date}`).                             |
| **Template File Path**  | Optional path to a template file (e.g., `.xlsx`) that provides a preformatted structure for the output file.                                         |
| **Template Sheet Name** | If using a template, this defines the sheet within the template where data should be inserted.                                                       |
| **Split By Column**     | When specified, the output data will be split by distinct values in this column, producing separate files or sheets. Useful for grouped file output. |
| **Start Row**           | The row index (1-based) in the output file or sheet from which the query results will begin writing.                                                 |
| **Start Column**        | The column index (1-based) in the output file or sheet where the data writing should begin.                                                          |

> All file-related fields are only rendered when **Save As File** is enabled.

## Sample Input

_Not Applicable_

## Sample Configuration 1

| Field         | Value                                        |
| ------------- | -------------------------------------------- |
| Datasource Id | `postgresql-sales-db`                        |
| Query         | `SELECT * FROM orders WHERE status = 'Paid'` |
| Save As File  | Diabled                                      |

## Sample Output

When **Save As File** is **Disabled**:

| order_id | customer_name | region | amount | status |
| -------- | ------------- | ------ | ------ | ------ |
| 1001     | John Smith    | East   | 120.50 | Paid   |
| 1002     | Alice Taylor  | West   | 89.00  | Paid   |

## Sample Configuration 2

| Field               | Value                                        |
| ------------------- | -------------------------------------------- |
| Datasource Id       | `postgresql-sales-db`                        |
| Query               | `SELECT * FROM orders WHERE status = 'Paid'` |
| Save As File        | Diabled                                      |
| File Pattern        | `paid_orders_{yyyyMMdd}.xlsx`                |
| Secret Key          | `securekey123`                               |
| Template File Path  | `/templates/order_template.xlsx`             |
| Template Sheet Name | `Orders`                                     |
| Sheet Name          | `Paid Orders`                                |
| Start Row           | 5                                            |
| Start Column        | 2                                            |
| Split By Column     | `region`                                     |

## Sample Output 2

When **Save As File** is **Enabled**: below data is written to sepcified file and downloadable file is populated

| order_id | customer_name | region | amount | status |
| -------- | ------------- | ------ | ------ | ------ |
| 1001     | John Smith    | East   | 120.50 | Paid   |
| 1002     | Alice Taylor  | West   | 89.00  | Paid   |
