Link to home
Start Free TrialLog in
Avatar of Phman Super
Phman SuperFlag for United States of America

asked on

Data Comparison and Update

Can anyone tell me how to compare 2 data tables and update the field if they are not match.

Example.
Table 1
Loan#       TaxReturn  BankStatement
123456     Y                  null
234567     N                 N

Table 2
Loan#       TaxReturn  BankStatement
123456     Y                  Y
234567     Y                  Y

if the Table1.TaxReturn not equal Table2.TaxReturn then use the Table2 value to update the Table1.
if the Table1.BankStatement not equal Table2.BankStatement then use the Table2 value to update the Table1.

it is ok to use Linq if the speed is faster.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why dont't use update sql command:

UPDATE table1
   SET 
      Table1.taskreturn=table2.taskreturn, 
       Table1.bankstatement=table.bankstement

   FROM table1
     INNER JOIN table2
         ON (table1.loan =Table2.loan)
   Where table1.taskreturn<>table2.taskreturn
Or table1.bankstatement<>table2.bankstatement

Open in new window

Linq:
from t1 in table1
join t2 in table2 on t2.loan equals t1.loan
where t1.taskreturn<>t2.taskreturn 
        || t1.bankstatement<>t2.bankstatement
select new 
{ t1.loan, 
   t1.taskreturn<>t2.taskreturn?t2.taskreturn : t1.taskreturn,
   t1.bankstatement<>t2.bankstatement?t2.bankstatement : t1.bankstatement
   },

Open in new window