I’m working on building an automated workflow in n8n that connects an AI assistant to a Microsoft SQL Server database. The setup is working fine and I can connect to the database without issues.
My goal is to let the AI create SQL queries dynamically and then execute them through the MSSQL database node. However, I’m stuck on what exactly should go in the Query parameter field to make this work.
I’ve tried several approaches including leaving it empty, using empty strings, and attempting expressions like {{ $fromAI('sqlQuery') }} and {{ $fromAI('query') }} but none of these seem to work correctly.
For reference, I successfully built a similar setup using Supabase where no query parameter was needed. Is there a specific syntax or expression that should be used in the Microsoft SQL node to accept AI-generated queries?
try {{ $json.query }} or {{ $input.first().json.query }} - if your AI node outputs the SQL in a ‘query’ field. also, check that the AI response is plain text, no extra formatting cus that could break the SQL execution.
The expression syntax depends on how your AI node outputs data. You’ll usually want to reference the previous node like this: {{ $('AI_Node_Name').first().json.sqlQuery }} where ‘AI_Node_Name’ is your actual AI node name and ‘sqlQuery’ is the field with your generated SQL. I ran into the same issues when I started - just make sure the field name in your expression matches exactly what the AI node spits out. Run the AI node first and check the output in the node inspector. Sometimes AI nodes wrap responses in different field names like ‘text’, ‘content’, or ‘response’ instead of ‘query’. One more thing - MSSQL node needs explicit query parameters unlike Supabase, so you can’t leave it empty.
Had this exact issue 6 months ago with a similar setup. Here’s what fixed it: the MSSQL node needs the query parameter filled out - it’s not like other database nodes that work without it. I used {{ $node["Previous_Node_Name"].json["field_name"] }} where Previous_Node_Name is your AI node’s actual name and field_name is whatever your AI spits out the SQL as. Here’s the annoying part - every AI node formats output differently. Some use ‘output’, others ‘text’ or ‘result’. You’ll need to dig into your AI node’s JSON output to find the right field name. Also, make sure your AI generates proper MSSQL syntax or you’ll get errors. Pro tip: hardcode something simple like ‘SELECT TOP 1 * FROM your_table’ first to test the MSSQL connection, then switch to dynamic queries once that works.