如何读取每一行并比较2表并将匹配字段保存到数据集中。 [英] How Do I Read Each Row And Compare 2 Table And Save The Matching Fields Into A Dataset .

查看:56
本文介绍了如何读取每一行并比较2表并将匹配字段保存到数据集中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比较两个表

table1和table2





匹配table2的行与table1进行复制数据集和数据表





一旦我有数据集(ds)和数据表(dt)



i想要将这些插入到table3

(条件:没有重复的条目。)



注意:table2是主要的钥匙和桌子3有外键..







谁能告诉我怎么能这样做。 ..在asp.net(csharp)

如果您有示例代码..请分享为例

解决方案

用户光标。 。

 声明  @ cl1   as   varchar  50 

声明 @ cl2 as varchar 50
声明 @ cl3 as varchar 50
声明 @ cl6 as varchar 50
声明 @ cl4 as varchar 250
声明 @ cl5 as varchar 50
声明 @ CT as int
set @ Ct = 0

创建 #temp(column1 bigint ,column2 varchar 50 ),column3 varchar 50 ),column4 varchar 50 ))
DECLARE cursor1 CURSOR FOR 选择 Pk,column2,column3 来自 table_1 其中条件
OPEN cursor1
FETCH NEXT FROM cursor1 INTO @ cl1 @ cl2 @ cl3
WHILE @@ FETCH_STATUS = 0
开始
set @ Ct = select count(*)来自 table_2 其中 Pk = @ cl1
if (@ Ct> 0)
begin
Insert into #temp
end
CLOSE cursor_for_clients
DEALLOCATE cursor_for_clients

end

选择 * 来自 #temp


compare two table
table1 and table2


matching rows of table2 with table1 to copy into a dataset and datatable


once i have that dataset(ds) and datatable(dt)

i want to insert those to table3
(condition: no duplicate entry .)

Note: table2 is primary key and table3 has foreign key..



can anyone tell me how can i do this... in asp.net(csharp)
if You have sample code.. please do share as an example

解决方案

User Cursor..

declare @cl1 as  varchar(50)

declare @cl2 as varchar(50)
declare @cl3 as varchar(50)
declare @cl6 as varchar(50)
declare @cl4 as varchar(250)
declare @cl5 as varchar(50)
declare @Ct as int
set @Ct=0

create table #temp (column1 bigint,column2 varchar(50),column3 varchar(50),column4 varchar(50))
DECLARE cursor1 CURSOR FOR select Pk,column2,column3 from table_1 where condition
OPEN cursor1
FETCH NEXT FROM cursor1 INTO @cl1,@cl2,@cl3
WHILE @@FETCH_STATUS = 0
begin 
set @Ct=select count(*) from table_2 where Pk=@cl1 
if(@Ct>0)
begin
Insert into #temp
end
CLOSE cursor_for_clients
DEALLOCATE cursor_for_clients

end

select * from #temp


这篇关于如何读取每一行并比较2表并将匹配字段保存到数据集中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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