How to configure n8n's AI-generated SQL queries for Microsoft SQL database?

Hey everyone,

I’m working on an n8n workflow that uses AI to generate SQL queries for a Microsoft SQL database. I’ve got the connection to the database working fine, but I’m stuck on how to make the AI create the query.

The Microsoft SQL node has a ‘Query’ parameter, and I think that’s where the AI-generated query should go. But I’m not sure what to put there to make it work.

I’ve tried a bunch of things like leaving it blank, using empty quotes, and trying different variations of $fromAI(), but nothing seems to do the trick.

Has anyone figured this out? What’s the secret sauce to get n8n to use AI-generated queries for MS SQL?

Thanks in advance for any help!

hey, i’ve tried this before. use the expression editor (the button next to Query) to insert something like: {{ $json[‘ai_generated_query’] }}. ensure a prior node creates the query and the AI outputs valid T-SQL. good luck!

I’ve actually been working with n8n’s AI-generated SQL queries for a while now, and I can share some insights from my experience. The key is to use the ‘Expression’ feature in n8n. In the Microsoft SQL node, click on the ‘Add Expression’ button next to the ‘Query’ field. This opens up the expression editor.

In the expression editor, you’ll want to use something like this:

{{ $node['OpenAI'].json['generated_query'] }}

This assumes you have an OpenAI node earlier in your workflow that generates the SQL query. You’ll need to adjust the node name and the exact path to the generated query based on your specific setup.

One thing to watch out for is ensuring your AI model is properly trained or prompted to generate valid T-SQL syntax for Microsoft SQL Server. It’s also a good idea to have some error handling in place, as AI-generated queries might not always be perfect.

Hope this helps you get your workflow up and running!

I’ve recently tackled this issue in my n8n workflows. The key is utilizing the expression editor for the ‘Query’ field in the Microsoft SQL node. Click the ‘Add Expression’ button and input something like:

{{ $node[‘AI_Query_Generator’].json[‘sql_output’] }}

This assumes you have a node named ‘AI_Query_Generator’ that produces the SQL query. Adjust the node name and output field as needed for your specific setup.

Ensure your AI model is properly configured to generate valid T-SQL syntax. It’s crucial to implement robust error handling and validation to catch any potential issues with the AI-generated queries before they reach your database.

Testing thoroughly in a non-production environment is advisable before deploying to live systems.