I’m setting up an n8n workflow to build an AI assistant that retrieves data from a Microsoft SQL database. The connection is working perfectly, but I’m having trouble figuring out the correct value for the ‘Query’ parameter in the Microsoft SQL node.
I’ve tried leaving the parameter blank, using empty quotes, and even using expressions like {{ $fromAI('') }}
and {{ $fromAI('query') }}
without success. I managed to create a similar AI agent with Supabase, which doesn’t require a query parameter, so I’m really at a loss here. Can anyone provide some guidance on the proper syntax or approach to get the AI to dynamically generate the SQL query?
Thanks in advance for any advice!
I’ve been working with n8n and AI-generated SQL queries for a while now, and I can share some insights that might help you out.
For the Microsoft SQL node, you’ll want to use an expression in the ‘Query’ parameter that references the output from your AI node. Assuming your AI node is named ‘OpenAI’ and it’s generating the SQL query, try something like this:
{{ $node[‘OpenAI’].json[‘generated_query’] }}
Make sure your AI node is actually outputting the SQL query in a field called ‘generated_query’ (or adjust the expression accordingly).
Also, don’t forget to properly sanitize and validate the AI-generated query before executing it on your database. You might want to add a Function node between the AI and SQL nodes to perform these checks.
Hope this helps! Let me know if you need any clarification or run into other issues.
hey sofia, i’ve been playin around with n8n and AI stuff too. for the MS SQL node, try this in the Query field:
{{ $node[‘YourAINodeName’].json[‘queryOutput’] }}
make sure ur AI node actually outputs the query. and yeah, definitely add some kinda check before running it on ur database. AI can be wonky sometimes lol. good luck!
Having worked extensively with n8n and AI integration, I can offer some advice on your setup. The key is to reference the output from your AI node correctly in the Microsoft SQL node’s ‘Query’ parameter.
Assuming your AI node (let’s say it’s named ‘AI_Query_Generator’) is producing the SQL query, you should use an expression like this:
{{ $node[‘AI_Query_Generator’].json[‘sql_query’] }}
Replace ‘AI_Query_Generator’ with your actual node name, and ‘sql_query’ with the specific output field containing the generated query.
A crucial step is to implement proper query validation and sanitization before execution. Consider adding a Function node to handle this, ensuring the AI-generated queries are safe and correctly formatted for your MS SQL database.
If you’re still facing issues, double-check the AI node’s output structure and make sure it’s generating valid SQL syntax for MS SQL specifically.