Data from 4 different database - sql

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")

Related

How to make queries for two different table placed in 2 different server

I just have two servers [sm33ooo.net] and [sm22ooo.net] , i would like to make query between two different tables (tbl_doc) in server [sm33ooo.net] and (tbl_archive) in [sm22ooo.net] using oracle sql developer ,any idea ?
you should be able to create a DBLINK on one of the databases which will allow you to query a table on that database from the other database. I'm not sure of your setup from your post so you might need to look into the difference between private and public dblinks to decide what is relevant to you.
For example if you add your link to server sm33ooo.net called dblink then on server sm22ooo.net you will be able to write select * from tbl_doc#dblink;

Query single server in SQL server group

I have 3 databases hosted across 3 different servers, all of which have different data and structures. I need to perform a query that will draw data from various tables across all three of them.
I've registered the three servers into a server group, and I've confirmed that all of the connections are working properly.
Here's an example of the problem I'm facing. For the first part of this query, I need to retrieve a list of records from the 'Applications' table in DB1 so I write:
SELECT * FROM [DB1].[dbo].[Application]
I know that this query works partially because it starts returning results from the correct table. The problem is that I haven't specified the server that DB1 is on, so once the query has finished querying DB1.dbo.Application, it looks for the same database and table on the next server. The database and table don't exist on the other servers so the query fails.
So how do I specify the server that I want the query to run on? I've tried [server_name].[DB1].[dbo].[Application], but it still runs the query across all of them.
Server groups are for maintenance purposes, what you need to use is a linked server
On one of the servers, say SERVER1, you will need to set up two linked servers - one to SERVER2 and one to SERVER3
From SERVER1 you will then be able to query the other servers using the four part name in a normal query window:
SELECT * FROM DatabaseName.dbo.Table1;
SELECT * FROM SERVER2.DatabaseName.dbo.Table2;
SELECT * FROM SERVER3.DatabaseName.dbo.Table3;
You can also use the tables from the remote server in JOINs etc as though they were on the local server and the remote servers don't even need to run SQL Server - they can be Oracle, MYSQL etc.
Be aware though, remote servers are slow and you may struggle with large datasets

data compare between two different sql servers

I am currently migrating data from an SQL 2008 R2 to SQL 2014 server and want to check that all data values in the destination server is identical to what is in the source server post migration. However, the two servers are on different network segments. What are some ways I can compare data between two different sql servers?
It depends on what you need to compare between the two databases, but you should have no problem creating a linked server to get what you need... just be sure to delete the linked server when you're done using it since it does pose a security concern.
http://learnsqlwithbru.com/2012/01/06/create-linked-server-to-connect-to-another-sql-server-part-i/
You could also use Service Broker which is more secure, but a bit more involved as well.
https://serverfault.com/questions/98084/can-i-setup-a-link-sql-server-connection-between-servers-on-different-networks
You can write a hash-function for each table individually. And then just compare the values.
Example of a simple implementation of this function for MSSQL:
SELECT
AVG(CAST(CAST(
HASHBYTES('MD5', CONCAT(columnn1, column2, column3))
AS BIGINT) AS FLOAT))
FROM simpleTable;
And the same for MySql:
SELECT
AVG(CONV(SUBSTR(MD5(CONCAT(column1, column2, column3)),
1, 16),
16, 10))
FROM simpleTable;

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

sql select thru multiple postgres databases

is there a way to create a select statement that would retrieve data from multiple database in postgre?
i was thinking it would be something like this:
select * from dbname1.table1, dbname2.table2 where dbname1.table1.column1 = dbname2.table2.column1
Have a look at the "dblink" contrib module.
OTOH it's possible that you treat the databases in a PostgreSQL cluster as equivalent to the databases in... let's say MySQL. Which is incorrect - the PostgreSQL databases contain schemas and those are the equivalent of the databases in MySQL.
From here:
It is not possible to access more than
one database per connection.
Update: but see Milen's answer.