Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query joining tables from different fdb databases

I have 2 fdb databases company.fdb and timeAtt.fdb

company.fdb contains staffDetail table

staffId       - 001
staffName     - Andy
staffStatus   - Active

timeAtt.fdbcontains staffAtt table

staffId         - 001
staffName       - Andy
timeIn          - 07:30
timeOut         - 04:30
LI              - X (late in)
AB              - X (absent )
remarks         - Emergency leave

Now, i would like to view the staff who was absent only which i did it this way

SELECT staffId,staffName,remarks FROM timeAtt.fdb WHERE AB = 'X'

But the problem is, the query also display inactive staff. So i need to join staffAtt from timeAtt.fdb and staffDetail from company.fdb to display only staff with active status. How can i do that?

like image 757
WaN Avatar asked May 19 '15 08:05

WaN


People also ask

Can you join 2 tables from different databases?

SQL Server allows you to join tables from different databases as long as those databases are on the same server. The join syntax is the same; the only difference is that you must fully qualify table names.

How do you sync tables in different databases?

Specifically, you can create a batch sync node, specify tables in multiple databases as the source tables, and then specify the destination table. After that, you can run the batch sync node to synchronize the data from the source tables to the destination table.

Can you query across databases in SQL Server?

Starting with SQL Server 2014 (12. x), memory-optimized tables do not support cross-database transactions. You cannot access another database from the same transaction or the same query that also accesses a memory-optimized table.


2 Answers

As Mark notes you cannot join them directly. But you can still use a DSQL statement to get what you want.

Use execute block and execute statement together. Here's a sample.

execute block
returning (
   staffId integer,
   staffName varchar(100),
   remarks varchar(100)
   staffStatus varchar(10))
as
begin
   for SELECT staffId, staffName, remarks 
   FROM timeAtt 
   WHERE AB = 'X'
   into :staffId, :staffName, :remarks do begin

      execute statement 'select staffStatus from company where staffId = ' || staffId
      on external "your:connection:\string\and\db.fdb" as user FOO password BAR
      into :staffStatus;

      suspend;
   end
end
like image 69
Paul Avatar answered Sep 21 '22 02:09

Paul


You can't. In Firebird you can only join tables in the same database file. Firebird 2.5 expanded EXECUTE STATEMENT to also execute a statement on an external datasource, but having a single query reference tables in different databases is not possible.

You have the following options:

  1. Create a temporary table, copy the data you need into that temporary table and then join to the temporary table,
  2. Merge the database into one.
like image 26
Mark Rotteveel Avatar answered Sep 22 '22 02:09

Mark Rotteveel