all groups > sql server programming > july 2005 >
You're in the

sql server programming

group:

2 databases with same tables..compare rowcounts


2 databases with same tables..compare rowcounts Hassan
7/21/2005 9:37:11 PM
sql server programming:
I have 2 databases with the same table names and want to compare rowcounts
between them. How can I do so ?

I guess the output should look like

DB1Name TableName Rowcount DB2Name TableName Rowcount
Status
DB1 T1 100 DB2 T1
100 Good
DB1 T2 100 DB2 T2
90 Fail


RE: 2 databases with same tables..compare rowcounts Enric
7/21/2005 11:43:08 PM
You must create a link between these two servers and then launch something
like this:

select count(*) from table1
union
select count(*) from server2.dbo.database2.table1
union
select count(*) from table2
union
select count(*) from server2.dbo.database2.table2

Regards,

[quoted text, click to view]
RE: 2 databases with same tables..compare rowcounts John Bell
7/21/2005 11:58:02 PM
Hi

You could do something like:

use tempdb
create table counts ( dbname sysname, tablename sysname, countval int )

use mydb1

INSERT INTO tempdb..counts ( dbname, tablename, countval )
exec sp_msforeachtable 'select db_name(),''?'',count(*) from ?'


use mydb2
INSERT INTO tempdb..counts ( dbname, tablename, countval )
exec sp_msforeachtable 'select db_name(),''?'',count(*) from ?'

Use tempdb

SELECT ISNULL(c1.dbname,c2.dbname),
ISNULL(c1.tablename,c2.tablename),
c1.countval,
c2.countval
FROM counts c1
FULL JOIN counts c2 ON c1.tablename = c2.tablename
WHERE c1.dbname = 'Mydb1'
AND c2.dbname = 'Mydb2'


John

[quoted text, click to view]
RE: 2 databases with same tables..compare rowcounts Enric
7/22/2005 12:03:01 AM
I was sleeping, sorry I thought -i don't know why- in different servers.

regards,

[quoted text, click to view]
RE: 2 databases with same tables..compare rowcounts John Bell
7/22/2005 3:55:05 AM
They may be, only the OP knows!!

[quoted text, click to view]
RE: 2 databases with same tables..compare rowcounts Enric
7/22/2005 4:17:01 AM
Dear John,
What did you mean? Sorry, I'm spanish and so many times I am not be able to
understand some "sentences". Could you be more explicit man?
regards,

[quoted text, click to view]
RE: 2 databases with same tables..compare rowcounts John Bell
7/22/2005 4:26:03 AM
Hi

The OP (original poster) did not specify where the databases were located,
it is an assumption that they are both on the same server.

John

[quoted text, click to view]
AddThis Social Bookmark Button