1

我正在尝试将两个 MS Access 表插入到一个 ms 访问表中。在两个表之一中,有一列包含重复值。我目前拥有的是:

 Dim MySQL As String = "INSERT INTO XML_TEST_CASE (" & _
                                    "TCParmId, " & _
                                    "TestCase, " & _
                                    "MessageType, " & _
                                    "FileNo,  " & _
                                    "InstructionNo,  " & _
                                    "TransactionNo,  " & _
                                    "ElementNo,  " & _
                                    "MessageSection, " & _
                                    "ElementLevel, " & _
                                    "FullElementPath, " & _
                                    "ElementValue, " & _
                                    "ElementValueNew, " & _
                                    "NameSpace, " & _
                                    "NameSpaceValue, " & _
                                    "Attribute, " & _
                                    "AttributeValue, " & _
                                    "TestCaseDescription) " & _
                                    "SELECT " & _
                                    "P.TCParmId, " & _
                                    "P.TestCase, " & _
                                    "P.MessageType, " & _
                                    "P.FileNo, " & _
                                    "P.InstructionNo, " & _
                                    "P.TransactionNo, " & _
                                    "S.ElementNo, " & _
                                    "S.MessageSection, " & _
                                    "S.ElementLevel, " & _
                                    "S.ElementPath + S.Element, " & _
                                    "S.ElementValue, " & _
                                    "S.ElementValue, " & _
                                    "S.NameSpace, " & _
                                    "S.NameSpaceValue, " & _
                                    "S.Attribute, " & _
                                    "S.AttributeValue, " & _
                                    "P.TestCaseDescription " & _
                                    "FROM XML_TEST_CASE_PARAMETER P, XML_MESSAGE_STRUCTURE S " & _
                                    "WHERE S.MessageType = P.MessageType " & _
                                    "AND P.TestCase = '" & MyTestCase & "' " & _
                                    "AND P.MessageType = '" & MyMessageType & "' " & _
                                    "AND P.FileNo = " & MyFileNo & " " & _
                                    "AND P.InstructionNo = " & MyInstructionNo & " " & _
                                    "AND P.TransactionNo = " & MyTransactionNo & ";"

问题是,在 XML_MESSAGE_STRUCTURE 表中,S.ElementNo 有时会出现多次,然后导致我在目标表中针对特定的 ElementNo 获得了多个记录。这是我不想要的。

任何帮助都会受到重视。

里科

4

1 回答 1

0

代替SELECT DISTINCT,您可以按除 ElementNo 之外的所有字段进行分组。对于 ElementNo,您可以选择Max(ElementNo)

 Dim MySQL As String = "INSERT INTO XML_TEST_CASE (" & _
                                "TCParmId, " & _
                                "TestCase, " & _
                                "MessageType, " & _
                                "FileNo,  " & _
                                "InstructionNo,  " & _
                                "TransactionNo,  " & _
                                "ElementNo,  " & _
                                "MessageSection, " & _
...
                                "SELECT " & _
                                "P.TCParmId, " & _
                                "P.TestCase, " & _
                                "P.MessageType, " & _
                                "P.FileNo, " & _
                                "P.InstructionNo, " & _
                                "P.TransactionNo, " & _
                                "Max(S.ElementNo), " & _
                                "S.MessageSection, " & _
...
                               "FROM XML_TEST_CASE_PARAMETER P, XML_MESSAGE_STRUCTURE S " & _
                                "WHERE S.MessageType = P.MessageType " & _
                                "AND P.TestCase = '" & MyTestCase & "' " & _
                                "AND P.MessageType = '" & MyMessageType & "' " & _
                                "AND P.FileNo = " & MyFileNo & " " & _
                                "AND P.InstructionNo = " & MyInstructionNo & " " & _
                                "AND P.TransactionNo = " & MyTransactionNo & _
                                "GROUP BY " & _
                                "P.TCParmId, " & _
                                "P.TestCase, " & _
                                "P.MessageType, " & _
                                "P.FileNo, " & _
                                "P.InstructionNo, " & _
                                "P.TransactionNo, " & _
                                "S.MessageSection, " & _
...
                                 ";"
于 2013-02-27T12:59:39.000 回答