Query for multiple databases - sql-server-2000

Is there a way to write a single query so that if you are connected to database A it does one thing and if you are connected to database B it does another?
I am using SSMS 2008 to access SQL 2000 servers.

You can define views on database A and on database B with the same name and columns, but different construction. Then your query is just to select from the view.

Related

Cross-Database Query on Azure SQL Database

I am using Azure SQL Database and I have two databases. I want to select one table data on the first database and merge into another (conditional) table of second database.
Is there any SQL solution to solve my problem?

Need an SQL query to connect the two tables in the different databases but the databases are in different server

Need an SQL query to connect the two tables that are in two different database,the databases are located in different servers.I am using SQL 2005.
You need (and should be googling) Linked Servers
http://technet.microsoft.com/en-us/library/ms188279.aspx

How can I select data in the same query from two different servers and databases from SQL Server Management Studio?

How can I select data in the same query from two different databases that are on two different servers, one DB2 Server and the other a SQL Server?
On your sql server, set up a linked server to the db2 database.
Then write your query on sql server. I suggest that you use openquery for the db2 stuff. If you have to combine the data, populate a sql server temp table with the openquery results and work from there.
The reason I suggest this is performance. I have found that if you use this syntax
select somefields
from server.database.owner.table
where whatever
sql server will bring back the entire table from the linked server and apply the where clause afterwards.
You can set up a linked server http://support.microsoft.com/kb/222937
How to create a linked server

Query a Linked Server with Excel Connection

In the past, I have been querying various different databases (let's call them DB1, DB2, and DB3) by using Data->From Other Sources->From SQL Server in Excel. However, I want to be able to perform just one query (say, DBMaster) and using this to populate my data.
My thoughts: Use linked servers. I create DBMaster with linked servers to DB1, DB2, and DB3. However, I do not know how to access those individual databases from Excel. When I connect to DBMaster, I only get the tables on that database, not the tables on the linked servers. Does anyone know how I can do this?
Thank you!
You just need to connect your master database and exceute your query from there.
Just remember to put the server name before database name in your sql in FROM clause like :
[SERVER].[DATABASE].DBO.[TABLE_NAME]

Data from 4 different database

I need to get data from 3 different databases on one event command. Can anybody tell me any efficient way besides I am querying to all three different database servers in a row:-
Server 1 : Select * from ....
Server 2 : Select * from.....
and so on...
Thanks very much
Seeing as the question is marked TSQL:
Install the providers for the 'other' databases.
In SQL Server 2005, create a linked server to each database, and then simply query as though the 'other' databases were SQL Server databases.
If the databases are on the same server instance, they can be queried in the format "database_name.table_name.column_name" otherwise I would use Mitch's answer (linked servers can be queried in the format "server_name.database_name.table_name.column_name")