I’m stuck trying to set up an AI agent in n8n to query a Microsoft SQL database. The workflow and database connection are fine, but I can’t figure out how to make the AI generate the SQL query.
I’ve looked everywhere for docs on what to put in the “Query” parameter, but no luck. I thought it might be something simple like {{ $fromAI('<something>') }}, but I’m just guessing.
I’ve tried leaving it blank, using empty quotes, and a few variations of $fromAI, but nothing works. It’s frustrating because I got it working easily with Supabase, which doesn’t need a query parameter.
Any ideas on the correct syntax or approach for this? I feel like I’m close, but missing something obvious. Help would be much appreciated!
I’ve recently tackled a similar challenge with n8n and AI-generated SQL queries. The key is to use the ‘Execute Query’ node in combination with an AI node like OpenAI or GPT-3. First, set up your AI node to generate the SQL query based on your input. Then, in the ‘Execute Query’ node, use an expression to pull the generated query from the AI node’s output.
For example, if your AI node is named ‘Generate SQL’, you might use something like this in the ‘Execute Query’ node’s query field:
{{ $node[‘Generate SQL’].json[‘query’] }}
This assumes the AI node outputs the query in a ‘query’ field. Adjust the field name if needed. Also, ensure your AI prompt clearly specifies the expected output format and any database-specific requirements.
Remember to thoroughly test and validate the generated queries before running them on your production database. AI-generated SQL can sometimes produce unexpected results.
I’ve been working with n8n and AI-generated SQL queries for a while now, and I can share some insights. The trick is to use a combination of nodes to achieve what you’re after. Start by setting up an AI node (like OpenAI or GPT-3) to generate your SQL query. In this node, craft a prompt that clearly outlines the structure and requirements for your MS SQL database.
Next, use the ‘Execute Query’ node and link it to your AI node’s output. In the ‘Query’ field of the ‘Execute Query’ node, you’ll want to use an expression that references the AI node’s output. It might look something like this:
Replace ‘AI_Node_Name’ with whatever you’ve named your AI node, and adjust ‘generated_query’ to match the exact output field from your AI node.
One word of caution: always double-check the generated queries before running them on your live database. AI can sometimes produce unexpected results, so it’s crucial to validate the output first. Good luck with your project!
hey, i’ve been messin with n8n and AI queries too. try using the ‘AI’ node before ur ‘Execute Query’ node. in the AI node, set up ur prompt to generate SQL. then in ‘Execute Query’, use something like {{ $node[‘AI’].json[‘output’] }} for the query. might need to tweak it, but thats the basic idea. good luck!