以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]关于Filler语句的排除重复问题  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=88343)

--  作者:陌聲人
--  发布时间:2016/8/1 11:16:00
--  [求助]关于Filler语句的排除重复问题

我有A表和B表,每张表都有自己的设置的类似于主键列的ID列,想将A表中的数据用Filler语句搬到B表。

 

但在设置完排除重复值后,A表的所有数据都被搬了过去,因为ID列是没有重复的。

 

所以想问:在排除重复值的时候,如何将ID列不列入判断范围。


--  作者:大红袍
--  发布时间:2016/8/1 11:19:00
--  

 要用这种方法

 

Dim Cols1() As String = {"来源列一","来源列二","来源列三"}
Dim Cols2() As String = {"接收列一","接收列二","接收列三"}
For Each dr1 As DataRow In DataTables("表A").Select("")
    Dim dr2 As DataRow = DataTables("表B").find("第一列 = \'" & dr1("第一列") & "\' and 第二列 = \'" & dr1("第二列") & "\' and 第三列 = \'" & dr1("第三列") & "\'")
    If dr2 Is Nothing Then
        dr2 = DataTables("表B").AddNew()
    End If
    For i As Integer = 0 To Cols1.Length -1
        dr2(Cols2(i)) = dr1(Cols1(i))
    Next
Next


--  作者:陌聲人
--  发布时间:2016/8/1 12:07:00
--  

还是会全部复制过去。我的A表的数据是从外部数据源读下来的,读下来之后要把修改过的或新增的全部丢到B表去,而且不会替换掉B表原来的数据。

我换了您的代码,把ID列排除后,还是会把重复的搬过去。


--  作者:大红袍
--  发布时间:2016/8/1 12:08:00
--  

 实例发上来。


--  作者:大红袍
--  发布时间:2016/8/1 12:15:00
--  
Dim Cols1() As String = {"第一列","第二列","第三列"}
Dim Cols2() As String = {"第一列","第二列","第三列"}
For Each dr1 As DataRow In DataTables("表A").Select("")
    Dim filter As String = "1=1"
    For i As Integer = 0 To cols1.length-1
        If dr1.IsNull(cols1(i)) Then
            filter &= " and " & cols2(i) & " is null"
        Else
            Dim dc As DataCol = DataTables("表B").datacols(cols2(i))
            If dc.IsString Then
                filter &= " and " & dc.name & " = \'" & dr1(cols1(i)) & "\'"
            ElseIf dc.IsBoolean Then
                filter &= " and " & dc.name & " = " & dr1(cols1(i))
            ElseIf dc.IsNumeric Then
                filter &= " and " & dc.name & " = " & dr1(cols1(i))
            ElseIf dc.IsDate Then
                filter &= " and " & dc.name & " = #" & dr1(cols1(i)) & "#"
            End If
        End If
    Next
    Dim dr2 As DataRow = DataTables("表B").find(filter)
    If dr2 Is Nothing Then
        dr2 = DataTables("表B").AddNew()
    End If
    For i As Integer = 0 To Cols1.Length -1
        dr2(Cols2(i)) = dr1(Cols1(i))
    Next
Next

--  作者:陌聲人
--  发布时间:2016/8/1 18:00:00
--  

 

[此贴子已经被作者于2016/8/1 21:19:15编辑过]

--  作者:大红袍
--  发布时间:2016/8/1 18:51:00
--  
 参考5楼代码。
--  作者:陌聲人
--  发布时间:2016/8/3 9:58:00
--  

还是会所有都复制过去。上面两行代码我没有添加ID列,但判断重复的时候好像都被列去判断范围了。


图片点击可在新窗口打开查看此主题相关图片如下:qq拼音截图未命名.jpg
图片点击可在新窗口打开查看


--  作者:大红袍
--  发布时间:2016/8/3 10:26:00
--  
 上传实例,贴出代码。
--  作者:陌聲人
--  发布时间:2016/8/3 10:51:00
--  

AfterOpenProject:

Dim Cols1() As String = {"T28CT","T28UT","T28DL","T28UserId","T28DeptId","T28PointId","F390","F391","F392","F394","F395","F396","F397","F398","F399","F400","F401","F402","F403","F404","F405","F406","F407","F408","F409","F410","F411","F412","F413","F414","F415","F416","F417","F418","F419","F420","F421","F422","F4367","F4368","F4370","F4371","F4372","F4373","F4374","F4375","F4376","F4377","F4378","F4379","F4380","F4381","F4382","F5140","F5221","F5222","F5223","F5224","F5225","F5226","F5299","F5308","F5310","F5311","F5312","F5313"}
Dim Cols2() As String = {"T246CT","T246UT","T246DL","T246UserId","T246DeptId","T246PointId","F4362","F4363","F4364","F4366","F4383","F4384","F4385","F4386","F4387","F4388","F4389","F4390","F4391","F4392","F4393","F4394","F4395","F4396","F4397","F4398","F4399","F4400","F4401","F4402","F4403","F4404","F4405","F4406","F4407","F4408","F4409","F4410","F4411","F4412","F4414","F4415","F4416","F4417","F4418","F4419","F4420","F4421","F4422","F4423","F4424","F4425","F4426","F5265","F5293","F5294","F5295","F5296","F5297","F5298","F5300","F5315","F5317","F5318","F5319","F5320"}
For Each dr1 As DataRow In DataTables("T28").Select("")
    Dim filter As String = "1=1"
    For i As Integer = 0 To cols1.length-1
        If dr1.IsNull(cols1(i)) Then
            filter &= " and " & cols2(i) & " is null"
        Else
            Dim dc As DataCol = DataTables("T246").datacols(cols2(i))
            If dc.IsString Then
                filter &= " and " & dc.name & " = \'" & dr1(cols1(i)) & "\'"
            ElseIf dc.IsBoolean Then
                filter &= " and " & dc.name & " = " & dr1(cols1(i))
            ElseIf dc.IsNumeric Then
                filter &= " and " & dc.name & " = " & dr1(cols1(i))
            ElseIf dc.IsDate Then
                filter &= " and " & dc.name & " = #" & dr1(cols1(i)) & "#"
            End If
        End If
    Next
    Dim dr2 As DataRow = DataTables("T246").find(filter)
    If dr2 Is Nothing Then
        dr2 = DataTables("T246").AddNew()
    End If
    For i As Integer = 0 To Cols1.Length -1
        dr2(Cols2(i)) = dr1(Cols1(i))
    Next
Next
DataTables("T246").Save()

[此贴子已经被作者于2016/8/3 16:12:42编辑过]