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
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
- Microsoft, Transact-SQL syntax conventions,
"Multipart names":
server_name.[database_name].[schema_name].object_name; database_name "Specifies the name of a SQL Server database when the object resides in a local instance of SQL Server", and server_name "Specifies a linked server name or remote server name". - MySQL 8.4, Identifier Qualifiers: in the table of column references, "db_name.tbl_name.col_name": "Column col_name from table tbl_name of the database db_name".
- PostgreSQL, Overview (chapter "Managing Databases"): "When connecting to the database server, a client must specify the database name in its connection request. It is not possible to access more than one database per connection."
- PostgreSQL, postgres_fdw: "The postgres_fdw module provides the foreign-data wrapper postgres_fdw, which can be used to access data stored in external PostgreSQL servers."
- MySQL 8.4, FEDERATED Storage Engine Notes and Tips: "The remote server must be a MySQL server." And The FEDERATED Storage Engine: "The FEDERATED storage engine is not enabled by default in the running server".
- Microsoft, Linked servers (Database Engine): "Typically, you configure linked servers to enable the Database Engine to execute a Transact-SQL statement that includes tables in another instance of SQL Server, or another database product such as Oracle" and "Linked servers aren't available in Azure SQL Database."
- Microsoft, sp_addlinkedserver: among the supported sources, an "ODBC data source" through the "Microsoft OLE DB Provider for ODBC" (MSDASQL), with an example.
- MySQL, Introduction to MySQL Connector/ODBC: "The MySQL Connector/ODBC is the name for the family of MySQL ODBC drivers".
- Microsoft, Editions and supported features of SQL Server 2022 on Linux, among the unsupported features: "Linked servers to data sources other than SQL Server".
- PrestaShop 9, System requirements: "MySQL 5.7 minimum or MariaDB 10.2 minimum, a recent version is recommended".
- WooCommerce, Server requirements: "MySQL version 8.0 or greater, or MariaDB version 10.6 or greater".
- Reportman AI: home page, Deep Analysis section: "it runs multiple queries against your database, examines the results and, when needed, digs even deeper", "A structured report with conclusions, advice and forecasts up front", and the card "Several databases in one analysis": "Choose up to 3 schemas from your Agents, for example your ERP's and your online shop's. In each round the analyst decides which database each query goes to, reads the results from all of them and compares them in a single report." Plans: "Deep Analysis is included on every plan when using Local AI. With cloud models (Standard or Precision) it requires a Pro plan or higher."
Toni Martir, arquitecto de software · · sources checked on