# Agent executing the sql code he has created

**URL:** <https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665>\
**Category:** Questions\
**Tags:** node, workflow-building\
**Created:** [July 11, 2025, 10:42am UTC](https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665 "2025-07-11T10:42:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaskedLlama04](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/maskedllama04/32/62792_2.png) [@MaskedLlama04](https://community.n8n.io/u/MaskedLlama04)\
**Post date:** [July 11, 2025, 10:42am UTC](https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665/1 "2025-07-11T10:42:52Z")

</div>

## Describe the problem/error/question

Hello! New user here. I have been trying to create a multiagent environment where firstly, the user writes a question about a database (named superheroes) as an input. The first agent is going to take that input and rewrite it for the better (for example: how many dudes are there → How many heroes are in total). This agent is going to connect to the database to know how really is the database and how to upgrade the input (knowing the names of the columns or the formant). This seems to work from what I have seen. However, the problem comes with the second agent. This agent will take the output from the first agent (no code, only a well made string telling what to do) and write a SQL code for it so it can be executed on the database. However, the problem lays on the fact that in the second mysql node, what’s the query I should tell him to use? Or, is it possible to make it authomatic (the agent does it by its own?). I have tried to browse information but no luck with it.

## What is the error message (if any)?

Everything works except that second mysql node of Agent 2. Which tells me (using the input “How many dudes are there”.

You have an error in your SQL syntax on line 1 near ‘The total number of superheroes is 6895.’

Any idea what can it be? I put the workflow here too. I believe it must be something about variables / queries.

sql: The total number of superheroes is 6895., code: ER\_PARSE\_ERROR

## Information of n8n setup

- **n8n version:** latest version
- **Database** : mySQL
- **n8n EXECUTIONS\_PROCESS setting (default: own, main):** own
- **Running n8n via (Docker, npm, n8n cloud, desktop app):** npm locally
- **Operating system:** Windows 11

---

<div class="post-metadata">

**Author:** ![moosa](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/moosa/32/108437_2.png) [@moosa](https://community.n8n.io/u/moosa)\
**Post date:** [July 11, 2025, 8:11pm UTC](https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665/2 "2025-07-11T20:11:06Z")

</div>

hey @MaskedLlama04  
I think you should rewrite your prompt for 2nd agent.  
you can try prompt like:  
`You are an expert SQL assistant. Based on the following question, write only a valid MySQL query to run on the `superheroes`table. Do not explain anything. Return ONLY the SQL query. Question: {{ $json.output }}`

I hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [July 11, 2025, 8:31pm UTC](https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665/3 "2025-07-11T20:31:49Z")

</div>

Hey @MaskedLlama04 hope all is well, welcome to the community.

Please see if this workflow helps:

Here is the output of the chat:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/a/5/a572b8daf1de508de163d4ef48093115433d9bf9.png)

And here is the intermediate results:  
**Initial query** : “how many dudes are there”  
**SQL interrogation to yield enhanced query** : “DESCRIBE superheroes;”  
**Enhanced query** : “How many superheroes are there in the superheroes table?”  
**SQL interrogation based on the enhanced query** : “SELECT COUNT(\*) AS superhero\_count FROM superheroes;”  
**SQL interrogation result** : “superhero\_count:10”  
**User response** : “There are 10 superheroes in the superheroes table.”

---

<div class="post-metadata">

**Author:** ![withabdul](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/withabdul/32/179439_2.png) [@withabdul](https://community.n8n.io/u/withabdul)\
**Post date:** [July 11, 2025, 11:04pm UTC](https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665/4 "2025-07-11T23:04:11Z")

</div>

Hi @MaskedLlama04 instead of defining the sql query for the AI to understand, you can use `{{ $fromAI("query", "mysql query to run") }}` to simplifying the query that will used by the AI.  
the `query` means “what query that AI need to insert”, and `mysql ... run` is the description to make the AI understand what `query provider` that AI need to execute.  
Also, some AI model cannot understand perfectly without specific instructions, you can use OpenAI, or describe the instructions more detailed

---

<div class="post-metadata">

**Author:** ![MaskedLlama04](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/maskedllama04/32/62792_2.png) [@MaskedLlama04](https://community.n8n.io/u/MaskedLlama04)\
**Post date:** [July 14, 2025, 7:47am UTC](https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665/5 "2025-07-14T07:47:15Z")

</div>

Hello everyone! I have been trying your several solutions, and now at least it compiles. I have tried to do what @jabbson did and the results were upgraded. However, I see its sql code does not really take into account the structure of the database, which is supossed to be learnt at the first agent. Anyone knows how?

What @withabdul and @moosa said about the query and prompt is correct too, thank you. The kind of error it gives me now tho is

```auto
Table 'mysql.dc_comics_characters' doesn't exist
sql: SELECT id FROM dc_comics_characters GROUP BY eye_color HAVING COUNT(*) > 10, code: ER_NO_SUCH_TABLE

```

When for example the database is named superheroes, there is no “dc\_comics\_characters” field and things like that. Forgot to say I really need to work with ollama, so I dont know how much that limits the idea.

---

<div class="post-metadata">

**Author:** ![moosa](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/moosa/32/108437_2.png) [@moosa](https://community.n8n.io/u/moosa)\
**Post date:** [July 14, 2025, 1:40pm UTC](https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665/6 "2025-07-14T13:40:48Z")

</div>

Then you should give structure of tables in system prompt too.

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [July 14, 2025, 3:30pm UTC](https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665/7 "2025-07-14T15:30:10Z")

</div>

It all comes down to prompt fiddling. If you really need the agent to learn the db structure, teach it how to do it, enforce certain actions.

- Run `SHOW DATABASES;` to …
- Run `SHOW TABLES;` to…
- Run `DESCRIBE table_name;` or `SHOW COLUMNS FROM table_name;` to…
- Run `SHOW CREATE TABLE table_name;` to…

triggers…  
indexes…  
procedures…  
table data samples…  
data from `INFORMATION_SCHEMA`…

etc… There are so many things you can propose the agent does in order to understand the structure, just give it a little push.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [July 21, 2025, 3:30pm UTC](https://community.n8n.io/t/agent-executing-the-sql-code-he-has-created/147665/8 "2025-07-21T15:30:52Z")

</div>

Closed on July 21, 2025, 3:30pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
