← All N8N workflows
AI & researchAgentChat TriggerConvert To FileExtract From FileIf

Generate SQL queries from schema only - AI-powered

Configured with Agent, Chat Trigger, Convert To File, Extract From File, and additional nodes. Review the connections and setup before adapting this template.

Workflow preview
AI & modelsOpenAI Chat ModelOpenAI Chat ModelData storeWindow Buffer MemoryMemory Buffer WindowRoutingNo Operation, donothingNo OpData storeList all tables in adatabaseMy SqlData storeExtract database schemaMy SqlTransformAdd table name tooutputEdit FieldsTransformConvert data to binaryConvert To FileFiles / mediaSave file locallyRead Write FileTransformExtract data from fileExtract From FileTriggerChat TriggerChat TriggerAI & modelsAI AgentAgentTriggerWhen clicking "Testworkflow"Manual TriggerTransformCombine schema data andchat inputEdit FieldsFiles / mediaLoad the schema fromthe local fileRead Write FileTransformExtract SQL queryEdit FieldsRoutingCheck if query existsIfTransformFormat query resultsEdit FieldsData storeRun SQL queryMy SqlTransformPrepare final outputEdit FieldsTransformCombine query resultand chat answerMerge

Node categories in this workflow

  • AI & models
  • Data store
  • Routing
  • Transform
  • Files / media
  • Trigger

Icons describe general node roles, inferred from node types. Inspect the parameters for the configured action.

Drag to pan; use + and − to zoom. Reconstructed from the exported node positions; nothing runs here. Dashed lines represent non-main connections. Full node names and notes appear below.

How it works

The exported workflow contains the following configured nodes. Their connections are shown above; inspect the JSON for the complete logic, expressions, and external actions.

Workflow node names, types, and operations
NodeCategoryTypeOperation
OpenAI Chat ModelAI & modelsOpenAI Chat ModelSee node parameters
Window Buffer MemoryData storeMemory Buffer WindowSee node parameters
No Operation, do nothingRoutingNo OpSee node parameters
List all tables in a databaseData storeMy SqlexecuteQuery
Extract database schemaData storeMy SqlexecuteQuery
Add table name to outputTransformEdit FieldsSee node parameters
Convert data to binaryTransformConvert To FiletoJson
Save file locallyFiles / mediaRead Write Filewrite
Extract data from fileTransformExtract From FilefromJson
Chat TriggerTriggerChat TriggerSee node parameters
AI AgentAI & modelsAgentSee node parameters
When clicking "Test workflow"TriggerManual TriggerSee node parameters
Combine schema data and chat inputTransformEdit FieldsSee node parameters
Load the schema from the local fileFiles / mediaRead Write FileSee node parameters
Extract SQL queryTransformEdit FieldsSee node parameters
Check if query existsRoutingIfSee node parameters
Format query resultsTransformEdit FieldsSee node parameters
Run SQL queryData storeMy SqlexecuteQuery
Prepare final outputTransformEdit FieldsSee node parameters
Combine query result and chat answerTransformMergeSee node parameters

Setup

  1. Download the JSON and import it into an isolated n8n instance. Leave the workflow inactive.
  2. Connect your own credentials through n8n’s credential store. Replace REPLACE_WITH_* values with your resource identifiers.
  3. Inspect API destinations, Code nodes, database queries, file paths, and outbound actions.
  4. Run with synthetic data and harmless destinations. Enable triggers only after checking the actual outputs.

Requirements

An n8n instance with the listed node types and compatible versions. Service-connected workflows may require accounts, credentials, or paid APIs.

Credential types declared in the source

  • mySql
  • openAiApi

Workflow notes

Run this part only once

Run this part only once

This section:

  • loads a list of all tables from the database hosted on db4free
  • extracts the database schema for each table and adds the table name
  • converts the schema into a binary JSON format
  • saves the schema ./chinook_mysql.json file locally

Now you can use chat to "talk" to your data! 🎉

Pre-workflow setup

Pre-workflow setup

Connect to a free MySQL server and import your database. Follow Step 1 and 2 in this tutorial for more.

The Chinook data used in this workflow is available on GitHub.

On every chat message:

On every chat message:

  • The workflow gets the data from the local schema file and extracts it as a JSON object. This way, we achieve two important improvements:
    • faster processing time as we don't need to fetch the schema for each table from a slow remote database
    • the Agent will know database structure without seeing the actual data
  • DB schema is then converted into a long string, JSON fields from the Chat Trigger are added before they are entered into the Agent node.
LangChain AI Agent's system prompt is modified.

LangChain AI Agent's system prompt is modified.

It uses only the database schema to generate SQL queries. The agent creates these queries but does not execute them. Instead, it passes them to subsequent nodes.

Example: "Can you show me the list of all German customers?"

Queries are generated only when necessary; for some requests, a query may not be needed. This is because certain questions can be answered directly without SQL execution.

Example: "Can you list me all tables?"

SQL query extraction

SQL query extraction

Check if the agent's response contains an SQL query. If it does, we extract the query using a regular expression.

Sticky Note7

The AI Agent remembers the schema, questions, and final answers, but not data values, since queries run externally. The agent can't access database content.

Sticky Note6
  • The SQL node accesses the database and executes the query. The results are then formatted for readability.
  • Both the chat response and the query result are displayed in the chat window.
Sticky Note8

When the agent responds without an SQL query, you receive an immediate answer with no additional processing.

Generate SQL queries from schema only - AI-powered

Generate SQL queries from schema only - AI-powered

Purpose and structure

This folder contains the Generate SQL queries from schema only - AI-powered workflow. Its executable nodes use agent, chatTrigger, convertToFile, extractFromFile, if, lmChatOpenAi, manualTrigger, memoryBufferWindow, merge, mySql, noOp, readWriteFile, set. The table and connection map below are derived from the JSON, not an inferred guarantee of behavior.

Setup

Import inactive. Configure your own credentials and replace REPLACE_WITH_* values. Review external destinations and Code nodes before running.

Entry points

Chat Trigger, When clicking "Test workflow"

Verification

Sanitized source candidate; not runtime-tested. This overview is generated from node metadata. Read the folder README for the connection map, credential types, and license.

Attribution

https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/workflows/Extractfromfile/1590_Extractfromfile_Converttofile_Create_Triggered.json MIT; preserve source notices.

More in AI & research