views:

2831

answers:

4

I am re-designing an application for a ASP.NET CMS that I really don't like. I have made som improvements in performance only to discover that not only does this CMS use MS SQL but some users "simply" use MS Access database.

The problem is that I have some tables which I inner join, that with the MS Access version are in two different files. I am not allowed to simply move the tables to the other mdb file.

I am now trying to figure out a good way to "inner join" across multiple access db files?

It would really be a pity if I have fetch all the data and the do it programmatically!

Thanks

+3  A: 

If you have access to the MDBs, and are able to change them, you might consider using Linked Tables. Access provides the ability to link to external data (in other MDBs, in Excel files, even in SQL Server or Oracle), and then you can perform your joins against the links.

I'd strongly encourage performance testing such an option. If it's feasible to migrate users of the Access databases to another system (even SQL Express), that would also be preferable -- last I checked, there are no 64-bit JET drivers for ODBC anymore, so if the app is ever hosted in a 64-bit environment, these users will be hosed.

John Rudy
Are you sure that relationships can be implemented with tables coming from different databases? I am not sure that an INNER JOIN instruction would be succesfull in such a situation (in fact I am convinced that you'll get an error!)
Philippe Grondier
Yes, Access has allowed arbitrary relationships between linked tables all the way back to the Windows 3.1 versions. However it often fetches the whole table on the fly, which can be a problem with large tables.
+1  A: 

Inside one access DB you can create "linked tables" that point to the other DB. You should (I think) be able to query the tables as if they both existed in the same DB.

It does mean you have to change one of the DBs to create the virtual table, but at least you're not actually moving the data, just making a pointer to it

Marc
A: 

Within Access, you can add remote tables through the "Linked Table Manager". You could add the links to one Access file or the other, or you could create a new Access file that references the tables in both files. After this is done, the inner-join queries are no different than doing them in a single database.

mistrmark
+6  A: 

You don't need linked tables at all. There are two approaches to using data from different MDBs that can be used without a linked table. The first is to use "IN 'c:\MyDBs\Access.mdb'" in the FROM clause of your SQL. One of your saved queries would be like:

SELECT MyTable.*
FROM MyTable IN 'c:\MyDBs\Access.mdb'

and the other saved query would be:

SELECT OtherTable.*
FROM OtherTable IN 'c:\MyDBs\Other.mdb'

You could then save those queries, and then use the saved queries to join the two tables.

Alternatively, you can manage it all in a single SQL statement by specifying the path to the source MDB for each table in the FROM clause thus:

SELECT MyTable.ID, OtherTable.OtherField
FROM [c:\MyDBs\Access.mdb].MyTable 
  INNER JOIN [c:\MyDBs\Other.mdb].OtherTable ON MyTable.ID = OtherTable.ID

Keep one thing in mind, though:

The Jet query optimizer won't necessarily be able to use the indexes from these tables for the join (whether it will use them for criteria on individual fields is another question), so this could be extremely slow (in my tests, it's not, but I'm not using big datasets to test). But that performance issue applies to linked tables, too.

David-W-Fenton
Wow. How often do you get to say that you learned something new about Access today? I haven't used Access in years, but it's painful to realize how much time this simple bit of knowledge would have saved me back then.
MusiGenesis
It's a shame that the Jet database engine has gotten such a tarnished reputation, as it's really a remarkable engine. Its ability to connect with so many different kinds of data (simultaneously) and make intelligent decisions about optimizing data retrieval is unequalled in any other platform.
David-W-Fenton