Databases / SQL Server
Natural language to SQL for SQL Server
NLQueries is an open source natural language to SQL engine for SQL Server. Ask a question in plain English, get SQL that is validated against your real SQL Server schema before it runs, and see the answer. It works from the command line, as a Python library, and as an MCP server for Claude Desktop, Cursor and other AI assistants.
Connect SQL Server and ask your first question
Install the package, register the connection once, build the knowledge base, and query. Passwords are prompted for interactively and stored in your OS keychain, never in a config file.
$ pip install "nlqueries-core[mssql]" $ nlqueries connect mssql --host my-server.database.windows.net --port 1433 --database mydb --user alice --alias sql-prod $ nlqueries process-history sql-prod --annotate && nlqueries export-kb sql-prod $ nlqueries query sql-prod "how many orders were placed last month by first-time customers?"
How NL2SQL works on SQL Server
Most text-to-SQL tools hand the LLM a schema dump and hope. NLQueries builds a YAML knowledge base from your SQL Server schema and from real query history, in this case sys.dm_exec_query_stats joined to sys.dm_exec_sql_text (needs VIEW SERVER STATE or VIEW DATABASE STATE), so it learns which joins your team actually uses and what your columns mean. That knowledge base is what the model sees when it writes SQL.
Every generated statement is parsed and checked column-by-column against the live schema with sqlglot before execution, then run inside a read-only transaction with a statement timeout. Use nlqueries ask to see the SQL without executing it, or nlqueries query to run it. A semantic cache answers repeated questions without another LLM or database round-trip.
SQL Server-specific details
One connector covers on-premises SQL Server and Azure SQL, since the T-SQL dialect is identical. For Azure SQL with SQL authentication, pass the user as alice@my-server.
If the login lacks VIEW SERVER STATE or VIEW DATABASE STATE, process-history returns nothing and the knowledge base is built from schema introspection only. Everything else still works.
Full connector notes are in the connectors guide, and the read-only role to create is in database hardening. This connector covers Microsoft SQL Server and Azure SQL Database.
Use it from Claude, Cursor or any MCP client
Run nlqueries mcp-server and point Claude Desktop, Cursor, or any Model Context Protocol client at it. The assistant can then query SQL Server through the same validated pipeline, with OIDC authentication and per-tool authorization available for network transports. See MCP authentication.
Frequently asked questions
How does NLQueries turn a natural language question into SQL?
It retrieves the relevant tables, columns, relationships and past query patterns from a YAML knowledge base built from your schema and query history, asks an LLM for SQL with that context, validates the SQL against your real schema with sqlglot before it runs, and executes it inside a read-only transaction with a statement timeout.
Is the generated SQL safe to run against production?
Generated SQL is validated column-by-column against the live schema, executed read-only with a timeout, and can be previewed with nlqueries ask without touching the database. Pair that with a least-privilege database role and it is designed for production use.
Does NLQueries support Azure SQL Database?
Yes. Azure SQL and SQL Server share the mssql connector. Use the alice@server form for the user with SQL authentication and grant VIEW DATABASE STATE if you want query-history capture.
Can Claude Desktop or Cursor query this database through NLQueries?
Yes. nlqueries mcp-server exposes the same pipeline as a Model Context Protocol server, so any MCP client can ask questions and get validated SQL results as a native tool call.
Other engines: Postgres · MySQL · Snowflake · BigQuery · Redshift · DuckDB · SQLite