| Author |
Topic |
|
vnarula99
Starting Member
6 Posts |
Posted - 2002-02-04 : 21:05:58
|
| Hi, I am in a problem, I have to compare two tables and find out which records are different from both the tables.I am trying to find out the fastest method for getting the results.Any help?? Vishal |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2002-02-04 : 21:13:49
|
| HiProbably the easiest way is download SQL Data Compare from www.red-gate.comEither that, or do an outer join between the two linked tables, with every column as a condition, but that sounds nasty.Damian |
 |
|
|
vnarula99
Starting Member
6 Posts |
Posted - 2002-02-04 : 21:24:21
|
| Using a third party tool not an option. I have thought of using the outer join but some of my tables has as many as 25 columns.Well anyway thanks for the reply-VishalVishal |
 |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2002-02-04 : 21:34:26
|
| Why isn't it an option ?Damian |
 |
|
|
vnarula99
Starting Member
6 Posts |
Posted - 2002-02-05 : 19:13:12
|
| I just found out Data Compare(Red-Gate.com) only works for SQL server 7.0I am using sql 6.5Vishal |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-02-05 : 21:07:38
|
| Depending on how often you're doing it, I can't see why you can't do:Select a.col1froma left join bon a.col1 = b.col1and a.col2 = b.col2and a.col3 = b.col3whereb.col1 is nulleven if you've got 25 columns it'll only take a couple of minutes to generate the code...is there some complicating factor?--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-02-05 : 21:15:02
|
quote: even if you've got 25 columns it'll only take a couple of minutes to generate the code...is there some complicating factor?
... and you could always doselect 'a.' + c.name + ' = b.' + c.name + ' and 'from syscolumns c inner join sysobjects oon c.id = o.id and o.name = 'a'to generate the join bits (don't forget to prune the last "and")--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
dsdeming
479 Posts |
Posted - 2002-02-07 : 13:27:53
|
| You could also try using the CHECKSUM or CHECKSUM_AGG functions. |
 |
|
|
vnarula99
Starting Member
6 Posts |
Posted - 2002-02-07 : 18:44:04
|
| Hi rrbIf i am not wrong the the outer joins will only give me the records which are present in one table and not in other, my aim is to find the differnce in two tables even if they both contain the same data.Thanks for the replyVishal |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-02-07 : 19:02:17
|
that's a terribly good point - sorry I was assuming they had the same rows and number of rows but with differences in their values.I assume then that you must have an id field to match on?? Otherwise how are you going to match them? It sounds like you're trying to handle replication without a GUID?can you send me the tabledefs for you two tables and a simple example of what you want returned?otherwise - all I can see isselect a.*, b.* from a left join bon a.col1 = b.col1and a.col2 = b.col2and a.col3 = b.col3whereb.col1 is nullunionselect a.*, b.* from b left join aon b.col1 = a.col1and b.col2 = a.col2and b.col3 = a.col3wherea.col1 is nulland that's pretty useless without some kind of id field to match on...--I hope that when I die someone will say of me "That guy sure owed me a lot of money"Edited by - rrb on 02/07/2002 19:04:24 |
 |
|
|
vnarula99
Starting Member
6 Posts |
Posted - 2002-02-07 : 19:55:48
|
| Hi rrbBelow is the example and the desired results that I am looking for Table AId1 Id2 Value1 Value21 1 aaa bbb2 2 ccc ddd3 3 eee fff4 4 ggg hhhTable BId1 Id2 Value1 Value21 1 aaa bbb2 2 ddd ddd5 5 iii jjjDesired resultsId1 Id2 Value1 Value22 2 ccc ddd (different Value record from Table A)2 2 ddd ddd (different Value record from Table A)3 3 eee fff (Not Present in Table B)4 4 ggg hhh (Not Present in Table B)5 5 iii jjj (Not Present in Table A)ThanksVishal |
 |
|
|
vnarula99
Starting Member
6 Posts |
Posted - 2002-02-07 : 20:01:46
|
Hi rrbI am posting the same reply as my earliar one.Below is the example and the desired results that I am looking for Table AId1 Id2 Value1 Value21 1 aaa bbb2 2 ccc ddd3 3 eee fff4 4 ggg hhhTable BId1 Id2 Value1 Value21 1 aaa bbb2 2 ddd ddd5 5 iii jjjDesired resultsId1 Id2 Value1 Value22 2 ccc ddd (different Value record from Table A)2 2 ddd ddd (different Value record from Table A)3 3 eee fff (Not Present in Table B)4 4 ggg hhh (Not Present in Table B)5 5 iii jjj (Not Present in Table A) ThanksVishal |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-02-08 : 01:04:57
|
OK Vishal here you go. Someone like nr might be able to do it easier.... select a.*, 'not present in table b' as reason into #afrom a left join bon a.id1 = b.id1 and a.id2 = b.id2 and a.value1 = b.value1 and a.value2 = b.value2whereb.id1 is null insert into #aselect b.*, 'not present in table a' as reason from b left join aon a.id1 = b.id1 and a.id2 = b.id2 and a.value1 = b.value1 and a.value2 = b.value2wherea.id1 is nullorder by a.id1update #aSet reason = 'differs from table ' + substring(reason, 22,1)from #a awhere id1 in (select id1 from (select id1, count(id1) as countid1 from #a group by id1 having count(id1) > 1) a )select * from #a order by id1drop table #aand remember to use the select 'a.' + c.name + ' = b.' + c.name + ' and 'from syscolumns c inner join sysobjects oon c.id = o.id and o.name = 'a'to create your join "on" conditions....Enjoy!--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
|