Currently I'm working on a simple library project using Embarcadero C++Builder 10.3 Community Edition, and Firebird and FlameRobin to create databases.
So far, I need only use simple queries, that were connected to a single database. Therefore, I used TFDConnection and TFDPhysFbDriverLink to connect to a .fdb file. Then, TFDQuery to create SQL commands and TDataSource. It works great.
Unfortunately, now I must join two tables. How do I write this command? I tried this:
SELECT * FROM users_books
join books on
users_books.id_book = books.id
where users_books and books are databases.
I got an error:
SQL error code = -204
Table unknown
BOOKS.
So I think I must connect somehow to these two databases simultaneously. How to do that?
Firebird databases are isolated and don't know about other databases. As a result, it is not possible to join tables across databases with a normal
selectstatement.What you can do, is use PSQL (Procedural SQL), for example in an
EXECUTE BLOCK. You can then useFOR EXECUTE STATEMENT ... ON EXTERNALto loop over the table in the other database, and then 'manually' join the local table usingFOR SELECT(or vice versa).For example (assuming a table
user_booksin the remote database, and a tablebooksin the current database):