Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,I have 2 tables Table1 and Table2 which have the same format of column for example both of them have following columns:
A  B  C  D  E  F  G  ...

where ID is the primary key.
Now how can i write a sql query to compare the data of them column by column and return the result as a datatable which shows each column of Table1 next to the same column of Table2?
I have found some ways in my searches such as using
SQL
minus ,Union All or Except
but my problem is sequence of columns in result.
Note : The number of columns in both tables is more than 130.

Edit : I mean i want something like this as the result:
A1  A2     B1  B2     C1  C2   ...
Posted
Updated 5-Nov-12 18:13pm
v4

Create two views and the dump the data in them. Make sure they have the same column structure.
Then compare the two views using UNION or EXCEPT just like you have done now.
 
Share this answer
 
Comments
M_Mogharrabi 6-Nov-12 0:26am    
Thnx, but i can not understand what you mean?
try this,
SQL
select * from
(
    select * from tbl1
    except
    select * from tbl2
) as a
   
union all

select * from
(
    select * from tbl2
    except
    select * from tbl1
) as a

Happy Coding!
:)
 
Share this answer
 
Comments
[no name] 5-Nov-12 6:14am    
Msg 205, Level 16, State 1, Line 1
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
Aarti Meswania 5-Nov-12 6:22am    
this error comes when Table1 & table2 both have not same no. of columns and type of columns Respectively
e.g.
table1
--------
col1 int
col2 varchar(10)

table2
-----------
col1 int

or

table2
---------
col1 int
col2 datetime

then it will give you error
because this will not be pass in rules for union all and except statements
Prasad Guduri 5-Nov-12 7:16am    
Thanks a Lot....!!!!
Aarti Meswania 5-Nov-12 7:56am    
welcome!
:)
M_Mogharrabi 6-Nov-12 0:27am    
Thnx Aarti Meswania for your reply, but it does not my solution, because your code does not set the sequence of columns in my preferred format.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900