data compare between two different sql servers - sql

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;

Related

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

Query SQL Linked Server only pulling data from one server

I have four SQL Servers that are named in the following way:
dbs
dbs2
dbs3
dbs4
I have a table that is on dbs3 called table1 in database1. This table does not exist on the other servers. However when I run the query:
select *
from dbs.database1.dbo.table1 (or any of the database servers)
it returns the results as if I queried the existing table on dbs3. It is like the DBMS is ignoring the 4 part nameing in the query and returning the results from table on dbs3 no matter which server I try to designate in the 4 part naming convention. Any ideas what could be going on here. The servers appear in the linked servers list.
If you can make changes without breaking stuff (or if it's already broken enough in your opinion), I recommend recreating your linked servers. If your linked server is another SQL Server, you can do
exec sp_dropserver 'dbs';
exec sp_addlinkedserver 'dbs';
This creates a linked server definition with the default configuration, which is appropriate for most applications (and can still be tweaked afterwards).

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 Server 2008, Sybase - large select queries over low bandwidth

I need to pull a large amount of data from various tables across a line that has very low bandwidth. I need to minimize the amount of data that gets sent too and fro.
On that side is a Sybase database, on this side SQL Server 2008.
What I need is to pull all the tables from the Sybase database that have to do with this office. Lets say I have the following tables as an example:
Farm
Tree
Branch
etc.
(one farm has many trees, one tree has many branches etc.)
Lets say the "Farm" table has a field called "CountryID", and I only want the data for where CountryID=12. The actual table structures I am looking at are very complex (and I am also not very familiar with them) so I want to try to keep the queries simple.
So I am thinking of setting up a series of views:
CREATE VIEW vw_Farm AS
SELECT * from Farm where CountryID=12
CREATE VIEW vw_Tree AS
SELECT * from Tree where FarmID in (SELECT FarmID FROM vw_Farm)
CREATE VIEW vw_Branch AS
SELECT * from Tree where BranchID in (SELECT BranchID FROM vw_Branch)
etc.
To then pull the actual data across I would then do:
SELECT * from vw_Farm into localDb.Farm
SELECT * from vw_Tree into localDb.Tree
SELECT * from vw_Branch into localDb.Branch
etc.
Simple enough to set up. I am wondering how this will perform though? Will it perform all the SELECT statements on the Sybase side and then just send back the result? Also, since this will be an iterative process, is it possible to index the views for subsequent calls?
Any other optimisation suggestions would also be welcome!
Thanks
Karl
EDIT: Just to clarify, the views will be set up in SQL Server. I am using a linked server using Sybase ASE to set up those views. What is worrying me in particular is whether the fact that the view is in SQL Server on this side and not on Sybase on that side will mean that for each iteration the data from the preceeing view will get pulled across to SQL Server first before the calculations get executed. I want Sybase to do all the calcs and just pass the results across.
It's difficult to be certain without testing, but my somewhat-relevant experience (using linked servers to platforms other than Sybase, and on SQL Server 2005) has been that using subqueries (such as your code for vw_Tree and vw_Branch) more or less guarantees that SQL Server will pull all the data for the outer table into a local temp table, then match it to the results of the inner query.
The problem is that SQL Server has no access to the linked server's table statistics, so can make no meaningful decisions about how to optimise the query.
If you want to be sure to have the work done on the Sybase server, your best bet will be to write code (could be views or stored procedures) on the Sybase side and reference them from SQL Server.
Linked server connections are, in my experience, not particularly resilient over flaky networks. If it's available, you could consider using Integration Services rather than linked-server queries - but even that may not be much better. You may need to consider falling back on moving text files with robocopy and bcp.

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