I’m working on building an AI workflow in n8n that connects to a Microsoft SQL Server database. The connection part works fine, but I’m stuck on one thing.
I want the AI to create SQL queries automatically and send them to the MS SQL node. The problem is I can’t figure out what to put in the Query field to make this work. I’ve looked around but there’s no clear documentation about this.
I think it might be something basic like {{ $fromAI('parameter') }} but I’m not sure what the exact syntax should be.
For context, I got this working with Supabase before and that was easier because it didn’t need a specific query parameter.
So far I’ve tested these values in the query field:
leaving it empty
""
''
{{ $fromAI('') }}
{{ $fromAI('query') }}
None of them worked and I feel like I’m just guessing at this point. Has anyone got this setup working before?
Check your AI node output first - click it after running to see the exact JSON structure. Then reference it like {{ $('previous_node_name').first().json.your_field_name }} in the query box. Don’t forget .first() - that’s what usually trips people up. Also make sure your AI isn’t wrapping the SQL in quotes or backticks.
Check if your AI node connects properly to the MS SQL node. The expression might be right, but the execution order could be wrong. I’ve seen the MS SQL node try to run before the AI node finishes - creates weird empty query errors. Make sure there’s a direct line between them or use the ‘Wait’ option. Some AI models add semicolons to SQL queries, which breaks certain MS SQL setups. If you’re still stuck, hardcode something simple like ‘SELECT 1’ in the MS SQL node to test your database connection, then work backwards.
Hit this same problem a few months ago with a similar setup. You need to use n8n’s standard expression syntax to grab the output from your AI node. If your AI node is called “OpenAI” or “AI Agent”, throw {{ $node["OpenAI"].json["sql_query"] }} into the Query field - just swap “sql_query” for whatever field has your generated SQL. n8n needs to know exactly which node and field to pull from. Your {{ $fromAI('query') }} won’t work - that’s not valid n8n syntax. Also check that your AI node spits out plain text SQL, not wrapped in markdown blocks or other formatting. I had to add text processing to strip that stuff out before feeding it to the SQL node.
click your ai node after it runs to see the actual json structure first. then just use {{ $json.sql }} in the ms sql query field (or watever the field’s called). don’t overthink it - n8n expressions r straightforward once you see what the output looks like.
To correctly configure the MS SQL node in n8n with AI-generated SQL commands, you should use {{ $json.query }} or {{ $('NodeName').first().json.query }}, depending on your specific configuration. The AI node produces JSON data, and your MS SQL node needs to refer to that data accurately. Ensure that the AI node outputs the SQL command as a distinct field, like ‘query’ or ‘sql’. If, for example, the output field is ‘generated_sql’, you should reference it as {{ $json.generated_sql }} in the Query parameter. It’s crucial to verify what the AI node is actually outputting by examining the execution data to determine the exact field name for your query.
Had this exact problem a few months ago. The trick is getting your AI node to output SQL as a proper JSON field first. I structured mine so the AI returns {“sql_command”: “SELECT * FROM table WHERE condition”} then used {{ $(‘AI_Node_Name’).first().json.sql_command }} in the MS SQL query field. You’ve got to get the node reference right - specify which node the data’s coming from instead of assuming n8n will figure it out. Also check your AI is generating valid SQL because n8n sometimes fails silently on syntax errors. I’d test with a hardcoded query first to make sure your MS SQL connection works, then troubleshoot backwards.
Run your AI node by itself and check what comes out in the execution panel. Your AI’s probably generating SQL fine, but the field name isn’t what you think it is. I’ve seen OpenAI nodes dump everything into a ‘text’ field instead of ‘query’ or ‘sql’. Once you find the real field name, use {{ $json.field_name }} for the previous node, or {{ $('specific_node_name').first().json.field_name }} to grab from a specific node. Watch out for AI models that wrap SQL in triple backticks - you’ll need a Set node between your AI and SQL nodes to strip those out with a replace function.
Been wrestling with this same setup recently. The problem’s usually how the AI node formats its output vs what MS SQL expects. Don’t guess with $fromAI() syntax - open your workflow execution view and expand the AI node results to see the actual JSON structure. Most AI nodes spit out something like {“content”: “SELECT * FROM users”}. Once you see the exact field name, reference it directly in your MS SQL query using {{ $json.content }} or whatever it’s called. Watch out for extra whitespace or formatting that breaks SQL execution - I throw in a trim function like {{ $json.content.trim() }} to clean it up before hitting the database.
chek that ur AI node outputs plain SQL text - not wrapped in JSON or markdown. I ran into this where my AI kept adding ``` around the SQL, which broke everything. debug it by dropping a code node after ur AI and console.log the output. you’ll see exactly what format it’s in and can reference it correctly in the MS SQL node.