| Author |
Topic  |
|
|
kalyan.cse05
Yak Posting Veteran
India
68 Posts |
Posted - 01/23/2013 : 01:10:06
|
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
47023 Posts |
Posted - 01/23/2013 : 01:15:31
|
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/
|
 |
|
|
vandana
Starting Member
25 Posts |
Posted - 01/23/2013 : 01:39:12
|
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 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 01/23/2013 : 01:43:10
|
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/
|
 |
|
|
kalyan.cse05
Yak Posting Veteran
India
68 Posts |
Posted - 01/23/2013 : 01:44:29
|
Thanks for the help but it is giving syntax error. However is there any thing to compare two database?
kalyan Ashis Dey |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 01/23/2013 : 01:47:46
|
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/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 01/23/2013 : 01:56:29
|
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/
|
 |
|
|
kalyan.cse05
Yak Posting Veteran
India
68 Posts |
Posted - 01/23/2013 : 02:14:18
|
same here.. though data is same in both table in different database, still showing "Table data is different"
kalyan Ashis Dey |
 |
|
|
vandana
Starting Member
25 Posts |
Posted - 01/23/2013 : 02:32:57
|
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'
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 01/23/2013 : 03:52:57
|
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/
|
 |
|
|
vandana
Starting Member
25 Posts |
Posted - 01/23/2013 : 04:09:28
|
sorry andd thanks it worked!!!
|
 |
|
|
karthik0805
Starting Member
14 Posts |
Posted - 01/23/2013 : 06:11:25
|
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' |
 |
|
|
kalyan.cse05
Yak Posting Veteran
India
68 Posts |
Posted - 01/23/2013 : 06:18:20
|
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 |
 |
|
|
karthik0805
Starting Member
14 Posts |
Posted - 01/23/2013 : 06:22:47
|
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' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 01/23/2013 : 07:10:31
|
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/
|
 |
|
| |
Topic  |
|