Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to find the difference between two tables?

Author  Topic 

joytwo0708
Starting Member

5 Posts

Posted - 2009-01-07 : 17:34:00
I have two same structure tables in SQL server 2005, I know tablediff.exe can tell the difference. But I want to save the diff result into a SQL table which should have the complete row from those two tables.

Like windiff, if open the diff result table, I can easily tell which row is new or updated or deleted.

How to do that?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-01-08 : 02:57:40

Insert into target_table(columns)
select columns from table1
except
select columns from table2

Insert into target_table(columns)
select columns from table2
except
select columns from table1


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-08 : 03:01:45
if below sql 2005, use left join or not exists

Insert into target_table(columns)
select t1.columns from table1 t1
left join table2 t2
on t2.pk=t1.pk
where t2.pk is null


Insert into target_table(columns)
select t1.columns from table1 t1
where not exists (select 1 from table2 where pk=t1.pk)


Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-01-08 : 03:04:27
if you are working In Sql 2005 then use the queries as Madhi Sir stated above like...

Insert into target_table(columns)
select columns from table1
except
select columns from table2

Insert into target_table(columns)
select columns from table2
except
select columns from table1

and if you are using Sql 2000 den you do it as,,,,


insert into target_table(columns)
SELECT column1 , column2....
FROM
(
SELECT A.column1 , A.column2...
FROM Tbl_1 A
UNION ALL
SELECT B.column1 , B.column2
FROM Tbl_2 B
) tmp
GROUP BY column1, column2
HAVING COUNT(*) = 1

Thanks.....
Go to Top of Page
   

- Advertisement -