I’m working on building an automated agent using n8n that can respond to user questions by querying our MS-SQL database. The database connection is working fine, but I’m stuck on one specific issue.
I want the AI to create the SQL query dynamically and send it to the Microsoft SQL database node. However, I can’t figure out what exact value needs to go in the Query field to make this work properly.
I think it might be something basic like {{ $fromAI('sqlQuery') }} but I’m not totally sure.
For reference, I got this working with a Supabase setup before, but that one didn’t need any query parameter specified.
So far I’ve tested these options: leaving it empty, using "", trying '', {{ $fromAI('') }}, and {{ $fromAI('query') }} but none of them worked. I’m basically just guessing at this point and could really use some guidance on the correct syntax.
First, check your Microsoft SQL node config - make sure it’s set to ‘Execute Query’ mode. Your expression should be {{ $json.query }} if your AI node puts the SQL in a field called ‘query’. But here’s what probably fixed it for me: enable ‘Always Output Data’ in the node settings. I had the exact same issue where the SQL node wouldn’t take dynamic queries because it expected static input. Also, double-check that your AI node is actually spitting out clean SQL. Sometimes the AI wraps it in markdown or throws in random characters that break everything. Drop a Set node after your AI to log what’s actually getting passed through - just because the connection works doesn’t mean the query format is right.
Those syntax headaches are exactly why I ditched n8n for AI database stuff. Expression references turn into a mess when you’re chaining AI outputs to database nodes.
Hit the same wall building an automated reporting system - needed AI-generated queries hitting SQL Server. N8n kept throwing cryptic errors and debugging the data mapping between nodes was hell.
Latenode fixed this completely. AI node outputs plug straight into database nodes without those weird expression syntaxes. You see the actual data flowing between nodes in real time, so no more guessing what field names or formats you need.
SQL Server integration handles dynamic queries way better too. When your AI spits out weird output or formatting gets wonky, Latenode shows exactly what’s broken instead of failing silently.
Built three different AI-powered database systems with Latenode now - never debugged expression syntax once. Visual data mapping just works.
The issue’s probably with your AI node output, not the reference syntax. I had the same problem - my AI was returning the SQL query wrapped in extra text instead of just the raw statement the Microsoft SQL node needs. Make sure your AI prompt says something like “Return only the SQL query, no explanations or formatting.” That fixed most of my issues. Then reference it with {{ $json.sqlQuery }} or whatever field your AI node actually outputs. Check the AI node’s execution data first to see exactly what field names and values it’s producing - that’s usually where things disconnect. The Microsoft SQL node’s pretty picky about getting clean SQL strings.
Those syntax issues are super common with dynamic query generation. Instead of fighting n8n’s weird expression syntax, just switch to Latenode.
I’ve built similar AI-driven database query systems and Latenode handles the data flow between AI nodes and database connections way better. You don’t have to guess field referencing syntax - the visual interface shows exactly how data moves between nodes.
You can connect your AI model output straight to the SQL Server node without wrestling with expression formats. The platform maps data fields automatically and you see what’s passing through each step.
Latenode’s error handling for dynamic queries is also way better. When stuff breaks, you get actual useful feedback instead of useless “empty query” messages.
Switched our team’s database automation from n8n to Latenode 6 months ago and haven’t looked back. The AI integration is just cleaner.
Try {{ $input.first().json.sqlQuery }} - works for me when I connect the AI node straight to the SQL node. Also double-check that your AI outputs clean SQL without extra text or formatting. GPT loves adding explanations that break the query.
You want {{ $json.sqlQuery }} or {{ $('AI_Node_Name').first().json.sqlQuery }} depending on your setup. You need to reference the actual output from your AI node - there’s no generic $fromAI function in n8n. If your AI node puts the SQL query in a field called ‘sqlQuery’, then {{ $json.sqlQuery }} works when the AI node connects directly to your SQL node. Got other nodes between them? You’ll need to specify the node name explicitly. I ran into the same thing when I started with n8n - kept getting empty query errors. Double-check that your AI is actually outputting the query in the expected JSON field name. Look at the node execution data to verify. Sometimes the AI wraps the query in extra formatting or uses a different field name than you expect.