Author |
Topic |
kalyan.cse05
Yak Posting Veteran
74 Posts |
Posted - 2013-01-23 : 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
52326 Posts |
Posted - 2013-01-23 : 01:15:31
|
do a check likeIF EXISTS(SELECT 1FROM(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'ELSEPRINT 'Table data same' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
vandana
Starting Member
29 Posts |
Posted - 2013-01-23 : 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
52326 Posts |
Posted - 2013-01-23 : 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 MVPhttp://visakhm.blogspot.com/ |
|
|
kalyan.cse05
Yak Posting Veteran
74 Posts |
Posted - 2013-01-23 : 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
52326 Posts |
Posted - 2013-01-23 : 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 MVPhttp://visakhm.blogspot.com/ |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-23 : 01:56:29
|
ok there was a typo.see full illsutration belowdeclare @t1 table(id int,val varchar(10))declare @t2 table(id int,val varchar(10))insert @t1select 1,'xyz' union allselect 3,'rt' union allselect 4,'sdgvef' insert @t2select 2,'xyz' union allselect 3,'sdgvedvfg' union allselect 5,'qwerwq' IF EXISTS(SELECT 1FROM(SELECT id FROM @t1 EXCEPT SELECT id FROM @t2 )t UNION ALLSELECT 1FROM (SELECT id FROM @t2 EXCEPT SELECT id FROM @t1 )u)PRINT 'Table data is different'ELSEPRINT 'Table data same'output-----------------------------------Table data is differentdeclare @t3 table(id int,val varchar(10))declare @t4 table(id int,val varchar(10))insert @t3select 1,'xyz' union allselect 3,'rt' union allselect 4,'sdgvef' insert @t4select 1,'xyz' union allselect 3,'rt' union allselect 4,'sdgvef' IF EXISTS(SELECT 1FROM(SELECT id FROM @t3 EXCEPT SELECT id FROM @t4 )t UNION ALL SELECT 1FROM (SELECT id FROM @t4 EXCEPT SELECT id FROM @t3 )u)PRINT 'Table data is different'ELSEPRINT 'Table data same'output------------------------------Table data same ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
kalyan.cse05
Yak Posting Veteran
74 Posts |
Posted - 2013-01-23 : 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
29 Posts |
Posted - 2013-01-23 : 02:32:57
|
Hi here is the sampleIF EXISTS(SELECT 1FROM(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'ELSEPRINT 'Table data same'select * from empIF EXISTS(SELECT 1FROM(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'ELSEPRINT 'Table data same' |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-23 : 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 MVPhttp://visakhm.blogspot.com/ |
|
|
vandana
Starting Member
29 Posts |
Posted - 2013-01-23 : 04:09:28
|
sorry andd thanks it worked!!! |
|
|
karthik0805
Starting Member
14 Posts |
Posted - 2013-01-23 : 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 EXCEPTSELECT * FROM B.DBO.XYZ)A)=0 SELECT 'SAME DATA'ELSESELECT 'DIFFERENT DATA' |
|
|
kalyan.cse05
Yak Posting Veteran
74 Posts |
Posted - 2013-01-23 : 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 - 2013-01-23 : 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 EXCEPTSELECT * FROM SERVER_2.DB_NM.DBO.XYZ)A)=0 SELECT 'SAME DATA'ELSESELECT 'DIFFERENT DATA' |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-23 : 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 following1. linked server: add one server as a linked server in other and use belowmore details herehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=1648922. OPENROWSET: Use distributed query based on OPENROWSEThttp://msdn.microsoft.com/en-us/library/ms190312.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|