How to configure n8n Microsoft SQL node to let AI generate queries automatically

I’m building an AI workflow in n8n that connects to a Microsoft SQL Server database. The setup works fine and I can connect to the database without issues.

My goal is to have the AI create SQL queries dynamically and send them to the MS SQL node. However, I’m stuck on what exactly should go in the Query field to make this work.

I’ve tried several approaches:

// Empty values
""
''
blank field

// AI function attempts  
{{ $fromAI('') }}
{{ $fromAI('sql_query') }}

For comparison, when I built a similar agent using Supabase, it worked automatically without needing to specify a query parameter.

Is there a specific syntax or function I should use to tell the Microsoft SQL node to accept AI-generated queries? Any guidance would be really helpful since I can’t find clear documentation on this.

You can’t generate SQL directly in the Microsoft SQL node - it only accepts static SQL or expressions from previous nodes. Here’s what works: use an AI node first to create your SQL, then feed that to the MS SQL node. In the Query field, put {{ $node["AI_Node_Name"].json.query }} (replace AI_Node_Name with whatever you called your AI node). Claude or GPT nodes work great for this. Just make sure your prompt tells the AI to return clean T-SQL only - no markdown or extra text. Supabase probably has built-in AI query generation, which is why it behaved differently.

Everyone’s giving you the right technical answer but honestly, this is exactly why I moved away from n8n for AI database workflows.

Sure, you can chain an AI node to generate SQL then pass it to the MS SQL node with expressions like {{ $json.query }}. But you’ll quickly hit the same walls I did - AI hallucinations creating invalid SQL, managing errors between nodes, and debugging broken chains.

I was building similar workflows where AI needed to query our production databases dynamically. The n8n approach works but gets messy fast when you need proper error handling and query validation.

Latenode solved this exact problem for me. Their AI database integration handles query generation and execution as one atomic operation. No chaining nodes, no expression syntax headaches, and built-in safeguards for malformed queries.

The biggest win? Their AI actually understands SQL Server dialect differences automatically. With n8n I was constantly fixing queries that worked in one database but failed in SQL Server because of syntax quirks.

Saved me weeks of debugging and maintenance. Way cleaner than managing multiple nodes for something that should be one operation.

your issue might be simpler than everyone’s making it. try {{ $('previous_node').first().json.generated_sql }} in the query field - just replace ‘previous_node’ with whatever generates your sql. had the same problem and it was just wrong reference syntax. make sure your ai node spits out clean sql without extra formatting or you’ll get execution errors.

Been there with this exact problem multiple times. The MS SQL node can’t generate queries - it just executes what you feed it.

Here’s the thing - n8n gets clunky fast when you chain AI nodes with database operations. You’re managing multiple nodes, handling errors between each step, and dealing with syntax issues.

I switched to Latenode for this workflow and it’s way cleaner. Their AI integration with SQL databases is built for dynamic query generation. You set up the AI to generate queries and execute them against your SQL Server in one flow without juggling nodes.

Latenode handles SQL dialect validation automatically, so you don’t get random syntax errors that break everything. Plus their error handling between AI and database operations is much more robust than chaining n8n nodes.

I’ve built several workflows where AI needs to query databases dynamically and Latenode just handles it better. Less setup, fewer failure points, and the AI integration actually works like you’d expect.

The MS SQL node needs a complete SQL string in the Query field - it can’t handle AI function calls directly. You’ll need to set up your workflow so an AI node generates the query first, then passes it to the SQL node. In your Query field, use {{ $json.query }} to grab the AI output from the previous node. Supabase probably has built-in AI query features, but with n8n you have to connect the nodes manually. Make sure your AI prompt asks for SQL Server syntax and tells the model to return just the executable SQL - no explanations or code blocks. Start with simple queries to test your connection.

The Microsoft SQL node needs an actual SQL string, not an AI function call. You need two steps: an AI node generates the query, then passes it to the MS SQL node. I always throw a Code node between them to clean up the generated query. AI responses often have extra characters or formatting that breaks SQL execution. In the Code node, strip markdown formatting and check basic SQL syntax before sending it to the MS SQL node. Reference it with {{ $node["Code"].json.cleanQuery }}. This saves me tons of debugging time when queries fail because of weird formatting from different AI models.

I hit the same issue with dynamic SQL in n8n. You’re missing that the AI-generated query has to come from a previous node, not generated inside the SQL node itself. I set up a separate AI node (OpenAI or whatever) that creates the SQL query from your input, then reference that output in the Microsoft SQL node’s Query field with {{ $json.sql_query }} or whatever key your AI node outputs. The Microsoft SQL node doesn’t generate AI queries - it just runs whatever SQL you give it. Structure it like: Input → AI Query Generator → MS SQL Execution. Make sure your AI prompt generates valid SQL Server syntax since dialect differences will break execution.