N8n SQL Server Query

Published
Length
3 min read

Luis Felipe Dias

All posts

You run an n8n sql server query by linking a workflow node to your database. Your workflow or AI agent then sends SQL text and reads the returned rows. Teams use this setup to pull reports, update records, and feed live data into chat models. Setting up the link is fast, but keeping the data safe takes extra care. How an n8n sql server query works in practice In a standard workflow, an engineer adds a database node and enters server credentials. The node logs in, runs the SQL statement, and

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You run an n8n sql server query by linking a workflow node to your database. Your workflow or AI agent then sends SQL text and reads the returned rows.

Teams use this setup to pull reports, update records, and feed live data into chat models. Setting up the link is fast, but keeping the data safe takes extra care.

How an n8n sql server query works in practice

In a standard workflow, an engineer adds a database node and enters server credentials. The node logs in, runs the SQL statement, and passes the output to the next step. Each step in the flow can filter, map, or send those rows to another app.

Modern teams also let AI agents run database queries on their own. The n8n AI Agent node pairs a chat model with one or more tool sub-nodes. The agent decides which tools to call to finish a task.

Every AI Agent node needs at least one attached tool sub-node to work. The old SQL Agent only supported MySQL, SQLite, and Postgres data sources. In n8n 1.82.0, the agent type setting was deprecated across the platform. Node version 1 was removed in n8n 3.0, so all agents now run as a Tools Agent.

To let an agent run an n8n sql server query, teams attach a Call n8n Workflow Tool. They can also use an MCP server tool, a Custom Code Tool, or an HTTP Request Tool. When a user asks a question, the chat model picks the tool and triggers the query.

Security and operational tradeoffs of static database roles

When n8n connects to SQL Server, it uses a database user identity. A database user is the identity of a login when it connects to a database. Admins often create a SQL user tied to a login in the master database. This setup is common when a user needs access to many databases on an instance.

For one or a few databases, admins can use a contained database user with a password. This user does not tie to a login in master, though admins must enable contained databases first. SQL Server supports 13 types of database users in total.

SQL Query Filtering + Kubernetes API Server Access: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To limit what n8n can do, admins assign fixed database roles to that user. Members of the read-only role can read all data from all user tables and views. Members of the write role can add, delete, or change data in all user tables. A deny-read role blocks reading user tables, views, or metadata, while a deny-write role blocks changes.

The owner role can run all maintenance tasks and can even drop the database. Admins cannot change the permissions assigned to fixed database roles. Every role, including the public role, has permission to connect to the database.

Static roles leave big gaps when workflows or AI agents run queries in production. First, the read-only role opens every user table and view in the database. If an agent writes a broad query, it pulls sensitive columns along with safe ones. It can read emails, phone numbers, card numbers, and national IDs from customer tables.

Second, a single shared credential in n8n hides who actually triggered the query. Every run in the database log shows the exact same service account name. If a workflow or prompt goes wrong and updates rows, teams lack a clear user trail.

Third, static credentials stay active at all times inside the workflow app. If an attacker steals that key, they get full read or write power right away. Adding a query layer that checks commands and rewrites matched fields in results fixes these gaps.

Govern SQL Server queries and responses in flight

Table grants decide which SQL Server objects a caller may touch. They do not check query intent or rewrite sensitive values in results across every client and agent. By design, hoop.dev sits inline in the TDS wire protocol next to SQL Server as a sidecar for your SQL Server. On every production batch from an engineer or AI agent, hoop.dev decides to pass, mask, block, or hold it for approval.

With hoop.dev guardrails and hoop.dev data masking, banned statements are refused or held for review, matched sensitive fields in supported result columns are masked in memory, and each decision is logged. Check out the code on GitHub.

Frequently asked questions

Can the built-in n8n SQL Agent connect to SQL Server directly?

No, the old SQL Agent only worked with MySQL, SQLite, and Postgres data sources. Since n8n 1.82.0, all agents work as a Tools Agent and use attached tool sub-nodes instead.

How do I make an n8n sql server query read-only?

You can place the SQL Server database user into the fixed read-only database role. You can also add the deny-write role so the user cannot add, change, or delete rows.

Why is a read-only SQL Server role not enough for AI agents?

The fixed read-only role lets the user read all data across every user table and view. Without field masking, query results will still return raw emails, phone numbers, and card numbers to the agent.

The query runs. The emails stay hidden.