I am developing a web application (using classic asp for now) that needs to query two tables on two separate databases on the same server.
I have been given separate application-level login/passwords for each database by the IT team which will be used in my code to connect.
I want to create a query that links two tables (one from each database) together, but not too sure how, and Google hasn’t helped me much either.
I’m assuming I have to create two connections to do this as I have two separate logins, but how then would I create a query that uses tables from each.
Just a note – our IT team will not at this stage link the two databases together. I’m pretty much stuck with what I have.
Hope you can help.
Mat
The typical syntax for a cross database query would be something like:
However, if you’re using 2005 or above, it’s well worth creating a synonym in the first database so that the reference to the second database isn’t peppered throughout your code (what if the database name changes later)?
Then your query becomes:
You may need to set permissions appropriately such that the user you are using to connect to the first database has access to the second. If the IT team simply grants the relevant access to both databases from the same server principal, that is the best way to achieve it. If you are stuck with having two totally separate logins then that sucks. But there isn’t any need to ‘link the databases together’ in order to run a cross-database query.