SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Comapre same table in 2 different database
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kalyan.cse05
Yak Posting Veteran

India
74 Posts

Posted - 01/23/2013 :  01:10:06  Show Profile  Reply with Quote
I have a same table 'XYZ' in two different database "A" and "B".

I need to check if data is same in the tables or not.
Please help.
Thanks in advance.

kalyan Ashis Dey

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 01/23/2013 :  01:15:31  Show Profile  Reply with Quote
do a check like

IF EXISTS
(
SELECT 1
FROM(SELECT PKCol FROM A.dbo.XYZ
     EXCEPT
     SELECT PKCol FROM B.dbo.XYZ
    )t
    UNION ALL
    (SELECT PKCol FROM B.dbo.XYZ
     EXCEPT
     SELECT PKCol FROM A.dbo.XYZ
    )u
)
PRINT 'Table data is different'
ELSE
PRINT 'Table data same'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vandana
Starting Member

29 Posts

Posted - 01/23/2013 :  01:39:12  Show Profile  Reply with Quote
Hi vishak ,

I tried the above query for practising but i got Table data is different even though i copied same table to other database
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 01/23/2013 :  01:43:10  Show Profile  Reply with Quote
quote:
Originally posted by vandana

Hi vishak ,

I tried the above query for practising but i got Table data is different even though i copied same table to other database


show your sample data please

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kalyan.cse05
Yak Posting Veteran

India
74 Posts

Posted - 01/23/2013 :  01:44:29  Show Profile  Reply with Quote
Thanks for the help but it is giving syntax error.
However is there any thing to compare two database?

kalyan Ashis Dey
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 01/23/2013 :  01:47:46  Show Profile  Reply with Quote
quote:
Originally posted by kalyan.cse05

Thanks for the help but it is giving syntax error.
However is there any thing to compare two database?

kalyan Ashis Dey


whats the error?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 01/23/2013 :  01:56:29  Show Profile  Reply with Quote
ok there was a typo.

see full illsutration below


declare @t1 table
(
id int,
val varchar(10)
)

declare @t2 table
(
id int,
val varchar(10)
)


insert @t1
select 1,'xyz' union all
select 3,'rt' union all
select 4,'sdgvef' 

insert @t2
select 2,'xyz' union all
select 3,'sdgvedvfg' union all
select 5,'qwerwq' 

IF EXISTS
(
SELECT 1
FROM(SELECT id FROM @t1
     EXCEPT
     SELECT id FROM @t2
    )t
    UNION ALL
SELECT 1
FROM
    (SELECT id FROM @t2
     EXCEPT
     SELECT id FROM @t1
    )u
)
PRINT 'Table data is different'
ELSE
PRINT 'Table data same'


output
-----------------------------------
Table data is different


declare @t3 table
(
id int,
val varchar(10)
)
declare @t4 table
(
id int,
val varchar(10)
)

insert @t3
select 1,'xyz' union all
select 3,'rt' union all
select 4,'sdgvef' 

insert @t4
select 1,'xyz' union all
select 3,'rt' union all
select 4,'sdgvef' 


IF EXISTS
(
SELECT 1
FROM(SELECT id FROM @t3
     EXCEPT
     SELECT id FROM @t4
    )t
    UNION ALL
	 
SELECT 1
FROM
    (SELECT id FROM @t4
     EXCEPT
     SELECT id FROM @t3
    )u
)
PRINT 'Table data is different'
ELSE
PRINT 'Table data same'


output
------------------------------
Table data same



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kalyan.cse05
Yak Posting Veteran

India
74 Posts

Posted - 01/23/2013 :  02:14:18  Show Profile  Reply with Quote
same here.. though data is same in both table in different database, still showing "Table data is different"

kalyan Ashis Dey
Go to Top of Page

vandana
Starting Member

29 Posts

Posted - 01/23/2013 :  02:32:57  Show Profile  Reply with Quote
Hi

here is the sample

IF EXISTS
(
SELECT 1
FROM(SELECT empno FROM Test2.dbo.emp
EXCEPT
SELECT dept FROM Test.dbo.emp
) as t
UNION ALL
(SELECT empno FROM Test2.dbo.emp
EXCEPT
SELECT dept FROM Test.dbo.emp
)
)
PRINT 'Table data is different'
ELSE
PRINT 'Table data same'

select * from emp

IF EXISTS
(
SELECT 1
FROM(SELECT PKCol FROM A.dbo.XYZ
EXCEPT
SELECT PKCol FROM B.dbo.XYZ
)t
UNION ALL
(SELECT PKCol FROM B.dbo.XYZ
EXCEPT
SELECT PKCol FROM A.dbo.XYZ
)u
)
PRINT 'Table data is different'
ELSE
PRINT 'Table data same'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 01/23/2013 :  03:52:57  Show Profile  Reply with Quote
how is this comparing same columns?
see my example. i used primary key everywhere. you're comparing between two different columns (dept and empno) which will obviously be having different values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vandana
Starting Member

29 Posts

Posted - 01/23/2013 :  04:09:28  Show Profile  Reply with Quote
sorry andd thanks it worked!!!
Go to Top of Page

karthik0805
Starting Member

14 Posts

Posted - 01/23/2013 :  06:11:25  Show Profile  Reply with Quote
quote:
Originally posted by kalyan.cse05

same here.. though data is same in both table in different database, still showing "Table data is different"

kalyan Ashis Dey



Hope this will help you.

IF(SELECT COUNT(1) FROM
(
SELECT * FROM A.DBO.XYZ
EXCEPT
SELECT * FROM B.DBO.XYZ
)A)=0
SELECT 'SAME DATA'
ELSE
SELECT 'DIFFERENT DATA'
Go to Top of Page

kalyan.cse05
Yak Posting Veteran

India
74 Posts

Posted - 01/23/2013 :  06:18:20  Show Profile  Reply with Quote
Thanks a lot..
Need one more help. Is there any way to compare data between 2 same database which reside in two different database server.

kalyan Ashis Dey
Go to Top of Page

karthik0805
Starting Member

14 Posts

Posted - 01/23/2013 :  06:22:47  Show Profile  Reply with Quote
quote:
Originally posted by kalyan.cse05

Thanks a lot..
Need one more help. Is there any way to compare data between 2 same database which reside in two different database server.

kalyan Ashis Dey



Try this.

IF(SELECT COUNT(1) FROM
(
SELECT * FROM SERVER_1.DB_NM.DBO.XYZ
EXCEPT
SELECT * FROM SERVER_2.DB_NM.DBO.XYZ
)A)=0
SELECT 'SAME DATA'
ELSE
SELECT 'DIFFERENT DATA'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 01/23/2013 :  07:10:31  Show Profile  Reply with Quote
quote:
Originally posted by kalyan.cse05

Thanks a lot..
Need one more help. Is there any way to compare data between 2 same database which reside in two different database server.

kalyan Ashis Dey


do you mean two tables in different server dbs?
if yes you need to use one of following

1. linked server: add one server as a linked server in other and use below
more details here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=164892

2. OPENROWSET: Use distributed query based on OPENROWSET


http://msdn.microsoft.com/en-us/library/ms190312.aspx


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000