Reportman.AI Logo Reportman.AI

← All articles

Can the AI generate SQL across two different databases (ERP and online shop)?

· Toni Martir, arquitecto de software

A sports shop sells over the counter, through an ERP on a SQL Server at the office, and online, through PrestaShop or WooCommerce, which according to their requirements run on MySQL or MariaDB. The owner asks the AI: "in October, which items sold more on the website than over the counter?"

The AI translates the question into an SQL statement (the technical instruction that pulls the data out), and each statement is run by a single database server. One statement covering both databases is only possible if that server can reach the other one. If not, comparing them takes an AI that works in several steps: one statement per database and then reading both results together.

"Which items sell more online than over the counter?": who can answer it

One step, with the shop linked to the ERP One question, one statement using tables from both databases
compares: yes needs: the link
One step, no link One question, one statement that only sees one database
compares: no risk: counter sales only
Several steps, no link One statement per database; the AI reads both results and asks again if something doesn't add up
compares: yes needs: nothing new
A connector copies web sales into the ERP If it copies all of them, both kinds of sale are in one database
compares: yes needs: the connector

The ERP runs on SQL Server; the shop, on MySQL. In every case you have to know which shop item is which item in the ERP.

One statement, one server

SQL Server and MySQL read another database on the same server by putting its name in front of the table. PostgreSQL works with one database per connection, and reading another takes postgres_fdw. Reaching another server means linking it, and the built-in links only join servers of the same make: postgres_fdw between PostgreSQL servers, and FEDERATED between MySQL servers, where it is off by default.

Across different makes, SQL Server on Windows can set up another database as a linked server through ODBC, a standard kind of driver that MySQL provides. On Linux, its linked servers only reach other SQL Servers, and Azure SQL Database has no linked servers at all.

Without a link, a one-step tool (question, statement, result) sees a single database. If it only knows the ERP's schema, it can write a statement that works, with the counter sales, and hand it over as if it were the answer.

Comparing without a link takes several steps

What doesn't fit in one statement is done in several. This is what the trade calls a multi-step architecture, or an agent: the AI writes one statement for the ERP and another for the shop, each runs on its own database, and the AI reads both results. If a piece of data is missing or something doesn't add up, it asks for another statement in a new round, and at the end it compares.

For the comparison to be reliable, each statement should already bring back the totals per item, worked out by its own database. That way the AI compares a few figures instead of adding up lists in its head, which is best avoided, as I explained in can you trust the AI's calculations?

First things first: which item is which

Every one of these approaches needs each item to be recognisable in both databases. If the shop stores the barcode and the ERP its own reference, you need a table that says which ERP reference matches each product on the website. That mapping is worth writing into the description of the tables, as I explained in what the AI needs to know about your database.

So can it?

A single statement only if both databases are on the same server or IT has linked one server to the other, and the AI knows both schemas. If not, for the AI to compare the ERP with the shop it needs a multi-step architecture: one statement per database and then reading the results together. And always knowing which shop item is which item in the ERP.


At Reportman AI, Deep Analysis can compare the ERP with the shop because it works in several steps: it runs several rounds of queries and opens a dataset from each database, with up to 3 schemas from your Agents at once. Since the AI receives the schemas, it doesn't spend steps finding out which tables exist, and it delivers a report with the conclusions first. With local AI it is included in every plan, and with cloud models from the Pro plan up, on tiered price plans.

Sources

Toni Martir, arquitecto de software · · sources checked on