# Text-to-SQL

> Source: https://netray.co/glossary/text-to-sql
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Glossary

## Short answer

Text-to-SQL is the technique of using a large language model to translate a plain-English question into a SQL query, run it against a database, and return the result. On ERP data it needs schema context, read-only access and query validation to be trustworthy.

## Aliases

- NL2SQL
- natural language to SQL
- natural language query

## Category

on-prem-ai

## Full Definition

A user asks, "Which open sales orders for customer ACME are past their promise date?" The model is given a description of the relevant tables and columns, writes a SELECT statement, the system executes it, and the rows come back as a table or a sentence. The model never invents the numbers, because they come from the database, not from the model's memory.

ERP schemas are the hard case. Infor SyteLine keeps data in tables such as job, jobmatl, co_item, item and itemwhse, with site-scoped keys and many user-defined fields. NetSuite exposes records through SuiteQL and saved searches rather than raw tables. Infor LN uses table names like tdsls400 and tcibd001 with company-number suffixes. Cryptic names mean a model without a curated semantic layer will guess wrong joins.

Production-grade text-to-SQL therefore adds a metadata layer: table and column descriptions, approved join paths, business definitions (what counts as "open" or "late"), and few-shot examples of verified queries. Retrieval picks only the relevant tables for each question so the prompt stays small and accurate.

Safety is the second half. The connection should be a read-only database role, queries are parsed and checked for allowed tables, row limits and timeouts are enforced, and the generated SQL is shown to the user so they can verify it. A model that can write UPDATE or DELETE against a live ERP database is a design failure, not a feature.

## Why It Matters

- It removes the report-request queue: planners and buyers get answers without waiting for IT to build a custom report.
- Answers are grounded in live ERP rows, which avoids the hallucinated figures that plague plain chatbots.
- Showing the generated query makes every answer auditable, which finance and quality teams require.
- Accuracy depends almost entirely on the semantic layer, so the real project work is documenting your schema, not choosing a model.

## In Practice

A planner asks for items where on-hand is below safety stock at site MAIN. The system retrieves the item and itemwhse tables, generates a SELECT joining them on item and whse, filters on qty_on_hand < safety_stock, limits to 200 rows, and displays both the SQL and the result. If the join path is wrong, the visible query lets the planner catch it in seconds, and the correction is saved as a verified example for next time.

## Faq

### Is text-to-SQL accurate enough for ERP data?

**Answer:** With a curated semantic layer, approved join paths and verified example queries it can be reliable for common questions. On a raw, undocumented schema accuracy drops sharply, so plan for a metadata-building phase.

### Can text-to-SQL change data in my ERP?

**Answer:** It should not. Use a read-only database role and validate that generated statements are SELECT only. Any write-back should go through the ERP's own APIs with human approval.

### Does it work with cloud ERPs that hide the database?

**Answer:** Often not directly. For NetSuite you query through SuiteQL or saved searches, and for other cloud ERPs you use their REST or OData APIs, so the model generates API calls instead of raw SQL.

### Does the model need to run on-prem?

**Answer:** Only the schema and question text go to the model, but results often contain sensitive rows. Regulated manufacturers typically run an open-weight model locally so no ERP data leaves the network.

## Search terms

text to sql erp, nl2sql, natural language query erp, llm sql generation, ask erp questions in english, text to sql syteline, text to sql netsuite, chat with database

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
