如何比较两个表的每个记录 [英] Hows compare each records of Two Table

查看:79
本文介绍了如何比较两个表的每个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的开发人员,
我必须比较2表.我想用c#或vb.net编码比较两个表的每个记录条目.如果从一个表到另一个表缺少任何记录,那么我将该记录插入到该表中. 如果有人共享用于实现此逻辑的小代码,那么任何人都会告诉我执行的逻辑比较两个表的每个记录,或者对我来说很有意义.

在两个表中都有3个列"Trans_Id","number","Amount"

谢谢,
Ravi Sharma

Dear Developer,
I have to compare 2 table. I want to compare the each record entry of the both table in c# or vb.net coding.If any record is miss from one table to other table then i insert that record on that table.
Any body tell wats logic i implement to compare the each record of 2 table or its greatful for me if anybody share small code for implementing this logic.

In both table have 3 coloum "Trans_Id", "number" ,"Amount"

Thanks,
Ravi Sharma

推荐答案

您始终可以运行只返回不同记录的查询:

You could always run a query that returns only the different records:

SELECT Trans_Id, number ,Amount FROM myTable1
EXCEPT
SELECT Trans_Id, number ,Amount FROM myTable2

这将返回myTable1中但不在myTable2中的记录



感谢您的答复,但这与c#编码有关."

然后这是一个简单的合并作业:

1)设置两个SqlCommand对象,每个表一个.
2)将按三个字段以相同顺序排序的每个表返回到单独的读取器中.
3)循环,构建两个需要添加的记录列表.
3a)如果一侧用完,则将另一侧的所有剩余记录添加到其列表中.
3b)如果表1当前记录大于表2,则将表2记录添加到表1列表中并前进表2.对表2执行相同的操作.如果它们相同,则继续前进.
4)当记录用完时,将适当的列表添加到适当的表中.

This returns the records that are in myTable1 but not in myTable2



"thanks for your reply, but this i have to do with c# coding."

Then it''s a simple merge job:

1) Set up two SqlCommand objects, one per table.
2) Return each table, ordered by the three fields in the same order, into a separate reader.
3) Loop round, building two lists of records that need to be added.
3a) If one side runs out, add all remaining records from the other side to it''s list.
3b) If Table1 current record is greater than Table2, add Table 2 record to Table1 list and advance Table 2. Do the same for Table2. If they are the same, move both on.
4) When you run out of records, add the appropriate list to the appropriate table.


这篇关于如何比较两个表的每个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆