# SQL Server - Query

> Execute SQL queries and stored procedures on Microsoft SQL Server databases.

Source: https://docs.sommatic.ai/en/nodes/integrations.microsoft.sqlserver.query/

- Slug: `integrations.microsoft.sqlserver.query`
- Family: Integrations
- Version: 1.0.0
- Status: Available
- Execution: async

Execute SQL queries and stored procedures on Microsoft SQL Server databases.

Connect your SQL Server instance (on-premise or Azure SQL) and run parameterized queries with @paramName binding for SQL injection prevention. Supports connection pooling and automatic cleanup.

**Operations:**
- **SELECT:** Fetch rows from tables with filters, joins, and aggregations.
- **INSERT:** Add new records to tables.
- **UPDATE:** Modify existing records matching a condition.
- **DELETE:** Remove records from tables.
- **Stored Procedures:** Execute stored procedures with named input parameters.
- **DDL:** Execute CREATE, ALTER, DROP statements for schema management.

## Configuration

- `connection_id` (string, required): Credential for your SQL Server / Azure SQL instance. Accepts credentials of type `connection-string` (`Server=...;Database=...;User Id=...;Password=...;`) or `basic-auth` (username + password + server + database).
- `sql` (string, required, accepts expressions): The SQL statement or stored procedure call to execute (e.g., "SELECT * FROM users WHERE id = @id").
- `parameters` (array, optional): Array of { name, type, value } objects for parameterized query binding.

## Output ports

- `out` (default): Resulting rows from the query
- `error`: Error occurred during execution

## Input contract

- `connection_id` (string, required)
- `sql` (string, required)
- `parameters` (array, optional)

## Output contract

- `result` (array<object>, optional): Array of rows returned by the query

## In expressions

What a later node can read from this node's output with `$("SQL Server - Query").item.json.<path>` (write your node's label instead of `SQL Server - Query`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("SQL Server - Query").item.json.result` (array) = `[…] (1)`: Array of rows returned by the query


## How it works

Runs one SQL statement on SQL Server or Azure SQL with named `@` parameters and returns the rows of the first record set under `result`.

1. Resolves the expressions of the node config. Empty `connection_id`, `sql` and `parameters` fall back to the field of the same name in the incoming item.
2. Resolves the credential on the server: a `connection-string` credential is passed to the driver as is; a `basic-auth` / `object` credential uses `username` (or `user`), `password`, `server` or `host`, `database` and `port` (default 1433) from the credential config or secret.
3. Connects with encryption on (off only for localhost) and trusts the server certificate.
4. Binds `parameters` as named inputs, runs `sql`, closes the connection and returns `{ result }` with the first record set (an empty array when the statement returns no rows).

## In a workflow

### Node

```json
{
  "node_id": "node-find-customer",
  "label": "Find customer",
  "operator_slug": "integrations.microsoft.sqlserver.query",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "sql": "SELECT id, email, status FROM customers WHERE id = @id",
    "parameters": [
      {
        "name": "id",
        "value": "{{ $json.customer_id }}"
      }
    ]
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": [
      {
        "id": 42,
        "email": "ana@example.com",
        "status": "active"
      }
    ]
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The statement ran. Queries with no rows and statements such as UPDATE also leave here with `result: []`.

```json
{
  "result": [
    {
      "id": 42,
      "email": "ana@example.com",
      "status": "active"
    }
  ]
}
```

### `error`

Validation, credential resolution, the connection or the statement failed.

```json
{
  "error": "Must declare the scalar variable \"@id\"."
}
```

## Behaviour in the code

- `parameters`: Accepts an array of `{ name, value }` or a plain object `{ "id": 42 }`. A `type` field is ignored: the driver infers the SQL type from the JavaScript value. Names are written without `@`.
- `connection_id`: A value containing `;`, `localhost` or `127.0.0.1` is parsed as inline JSON connection settings and the vault is skipped, so a raw ADO-style connection string placed here fails with a JSON parse error. Store connection strings as a `connection-string` credential.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `connection_id is required` | `error` | No credential in the config or in the incoming item. |
| `sql query is required` | `error` | `sql` is empty. |
| `Credential <connection_id> has unsupported type '<type>' for integrations.microsoft.sqlserver.query. Use 'connection-string' or 'basic-auth'.` | `error` | The credential type is not supported by this node. |
| `Must declare the scalar variable "@<name>".` | `error` | `sql` uses a parameter that is not in `parameters`. |
| `Login failed for user '<user>'.` | `error` | Wrong user or password in the credential. |

## Notes and limits

- Only the first record set is returned; `rowsAffected` and output parameters are not exposed. Add `OUTPUT` or a final `SELECT` to get values back from a write.
- A new connection is opened and closed on every run. The driver's default timeouts apply and the node does not retry.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "integrations.microsoft.sqlserver.query",
  "name": "SQL Server - Query",
  "category": "integrations",
  "group": [
    {
      "key": "microsoft",
      "name": "Microsoft",
      "icon_url": "/icons/integrations/microsoft/microsoft.svg"
    },
    {
      "key": "sql-server",
      "name": "SQL Server",
      "icon_url": "/icons/integrations/microsoft/sql-server.svg"
    }
  ],
  "icon_url": "/icons/integrations/microsoft/sql-server.svg",
  "version": "1.0.0",
  "description": "Execute SQL queries and stored procedures on Microsoft SQL Server databases.\n\n  Connect your SQL Server instance (on-premise or Azure SQL) and run parameterized queries with @paramName binding for SQL injection prevention. Supports connection pooling and automatic cleanup.\n\n  **Operations:**\n  - **SELECT:** Fetch rows from tables with filters, joins, and aggregations.\n  - **INSERT:** Add new records to tables.\n  - **UPDATE:** Modify existing records matching a condition.\n  - **DELETE:** Remove records from tables.\n  - **Stored Procedures:** Execute stored procedures with named input parameters.\n  - **DDL:** Execute CREATE, ALTER, DROP statements for schema management.",
  "capabilities": [
    "sql",
    "database",
    "mssql",
    "microsoft",
    "azure"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "SQL Server Credential",
        "description": "Credential for your SQL Server / Azure SQL instance. Accepts credentials of type `connection-string` (`Server=...;Database=...;User Id=...;Password=...;`) or `basic-auth` (username + password + server + database).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "connection-string",
            "basic-auth"
          ],
          "types": [
            "mssql"
          ]
        }
      },
      "sql": {
        "type": "string",
        "title": "SQL Query",
        "description": "The SQL statement or stored procedure call to execute (e.g., \"SELECT * FROM users WHERE id = @id\").",
        "format": "expression",
        "ui_widget": "sql_editor"
      },
      "parameters": {
        "type": "array",
        "title": "Query Parameters",
        "description": "Array of { name, type, value } objects for parameterized query binding."
      }
    },
    "required": [
      "connection_id",
      "sql"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "sql": {
        "type": "string"
      },
      "parameters": {
        "type": "array"
      }
    },
    "required": [
      "connection_id",
      "sql"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "array",
        "items": {
          "type": "object"
        },
        "description": "Array of rows returned by the query"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Rows",
      "description": "Resulting rows from the query",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Error occurred during execution",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "db-io"
  ],
  "tags": [
    "sql",
    "database",
    "microsoft"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://sommatic.ai",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "SQL Server Credential",
      "description": "Credential for your SQL Server / Azure SQL instance. Accepts credentials of type `connection-string` (`Server=...;Database=...;User Id=...;Password=...;`) or `basic-auth` (username + password + server + database).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "connection-string",
          "basic-auth"
        ],
        "types": [
          "mssql"
        ]
      }
    },
    "sql": {
      "type": "string",
      "title": "SQL Query",
      "description": "The SQL statement or stored procedure call to execute (e.g., \"SELECT * FROM users WHERE id = @id\").",
      "format": "expression",
      "ui_widget": "sql_editor"
    },
    "parameters": {
      "type": "array",
      "title": "Query Parameters",
      "description": "Array of { name, type, value } objects for parameterized query binding."
    }
  },
  "required": [
    "connection_id",
    "sql"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "sql": {
      "type": "string"
    },
    "parameters": {
      "type": "array"
    }
  },
  "required": [
    "connection_id",
    "sql"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "array",
      "items": {
        "type": "object"
      },
      "description": "Array of rows returned by the query"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Rows",
    "description": "Resulting rows from the query",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Error occurred during execution",
    "is_default": false,
    "supports_condition": false
  }
]
```
