SQL从旧表插入新表

SQL从旧表插入新表,sql,ms-access,vba,Sql,Ms Access,Vba,我有两个几乎相同的表,唯一的区别是新的表有不同的字段名。基本上,旧表已经过时,需要重新处理;我试图提取VendorNumber=Vendor\u ID的所有字段所有要复制到的字段都保证为空 下面的SQL代码非常大,但这是我能用的最好的了 SQL = "INSERT INTO tbleVendorData (EDIContact,EDIPhone,EDIEmail,EDIPlatform,EDIStatus,EDIMigrationStatus,EDIMigrationDate,TesterNam

我有两个几乎相同的表,唯一的区别是新的表有不同的字段名。基本上,旧表已经过时,需要重新处理;我试图提取VendorNumber=Vendor\u ID的所有字段所有要复制到的字段都保证为空

下面的SQL代码非常大,但这是我能用的最好的了

SQL = "INSERT INTO tbleVendorData (EDIContact,EDIPhone,EDIEmail,EDIPlatform,EDIStatus,EDIMigrationStatus,EDIMigrationDate,TesterName,TestStartDate,LastContactDate," & _
                  "CompletionStatus850,CompletionStatus856,CompletionStatus810C,CompletionStatus810F,CompletionStatusMH10,CompletionStatusPTicket,TestingNotes," & _
                  "TestingStatus850,TestingStatus855,TestingStatus856,TestingStatus810C,TestingStatus810F,GoLiveDate850,GoLiveDate855,GoLiveDate856," & _
                  "GoLiveDate810C,GoLiveDate810F,VendorKickOffDate,Exemption,ExemptionStartDate,ExemptionEndDate,ExemptionReason," & _
                  "ExemptionDescription,Ownership,NECC2500,NECC3500,NECC5000,NECC10000,NECCStartDate,NECCEndDate,SPSCertificate,ProductionStatus850," & _
                  "ProductionStatus855,ProductionStatus856,ProductionStatus810C,ProductionStatus810F,PlatformFI850,PlatformFI855,PlatformFI856," & _
                  "PlatformFI810C,PlatformFI810F,ProductionDate850,ProductionDate855,ProductionDate856,ProductionDate810C,ProductionDate810F," & _
                  "ProductionPrepack,ProductionCrossDock,ProductionMultiStyle,ProductionProductAttribute,ProductionMasterPack,ProductionCrossDockMix," & _
                  "ProductionStandAloneMix,ProductionLegalEntity,FirstASNDate,FirstINVDate,SALeadDays,CrossDockLeadDays,CommunicationNotes,ProductionNotes," & _
                  "QU1,QU2,ISA_ID1,ISA_ID2,GS_ID1,GS_ID2,VAN,VAN2,HYBRID) " & _
           "SELECT EDI_Contact_Name,EDI_Contact_Phone,EDI_Contact_Email,Platform,Status,Migration_Status,Migration_Date,Tester,Testing_Start_Date,Last_Contact," & _
                  "Status_850,Status_856,Status_810C,Status_810F,Status_MH10_Label,Status_PTicket,Tnotes,TS_850,TS_855,TS_856,TS_810C,TS_810F,GLD_850,GLD_855," & _
                  "GLD_856,GLD_810C,GLD_810F,Kick_Off_Mtg,Exemption,Exemption_Start_Date,Exemption_End_Date,Exemption_Text,Exemption_Reason,Ownership," & _
                  "NECC_2500,NECC_3500,NECC_5000,NECC_10000,NECC_Start_Date,NECC_End_Date,SPS_Certificate,PS_850,PS_855,PS_856,PS_810C,PS_810F,P850_FI,P855_FI," & _
                  "P856_FI,P810C_FI,P810F_FI,PDTS_850,PDTS_855,PDTS_856,PDTS_810C,PDTS_810F,Prepack,Cross_Dock,[M-style],PCA,Master_Pack,[XD-Mix],[SA-Mix],Legal_Entity," & _
                  "First_ASN,First_INV,Lead_Days,Cross_Dock_Lead_Days,CNotes,Info,QU1,QU2,ISA_ID1,ISA_ID2,GS_ID1,GS_ID2,VAN,VAN2,HYBRID " & _
             "FROM tbleVendorRecord " & _
            "WHERE tbleVendorRecord.Vendor_ID = VendorNumber "
我在VBA中使用MS Access并尝试运行上面的脚本,我得到了VendorNumber的输入参数值,知道为什么吗

编辑:


您需要指定旧表中的哪一列将插入到新表列中,还需要在查询中输入值:

SQL=插入tbleVendorData电子通讯录、EDIPhone…….&_ 值选择EDI_Contact_Name作为EDIContact,选择EDI_Contact_Phone作为EDIPhone….&_ 从tbleVendorRecord&_
其中tbleVendorRecord.Vendor\u ID=VendorNumber

由于目标表中已加载了要存储其记录的供应商的ID,因此需要更新这些记录,而不是插入新记录。因此,查询必须从供应商ID上连接到目标表的源表中选择行

SQL = "UPDATE tbleVendorData " & _
      "SET EDIContact = s.EDI_Contact_Name, EDIPhone = s.EDI_Contact_Phone, EDIEmail = EDI_Contact_Email, EDIPlatform = s.Platform, EDIStatus = s.Status," & _
          "EDIMigrationStatus = s.Migration_Status, EDIMigrationDate = s.Migration_Date, TesterName = s.Tester, TestStartDate = s.Testing_Start_Date, " & _
          "LastContactDate = s.Last_Contact, CompletionStatus850 = s.Status_850, CompletionStatus856 = s.Status_856, CompletionStatus810C = s.Status_810C, " & _
          "CompletionStatus810F = s.Status_810F, CompletionStatusMH10 = s.Status_MH10_Label, CompletionStatusPTicket = s.Status_PTicket, TestingNotes = s.Tnotes, " & _
          "TestingStatus850 = s.TS_850, TestingStatus855 = s.TS_855, TestingStatus856 = s.TS_856, TestingStatus810C = s.TS_810C, TestingStatus810F = s.TS_810F, " & _
          "GoLiveDate850 = s.GLD_850, GoLiveDate855 = s.GLD_855, GoLiveDate856 = s.GLD_856, GoLiveDate810C = s.GLD_810C, GoLiveDate810F = s.GLD_810F, " & _
          "VendorKickOffDate = s.Kick_Off_Mtg, Exemption = s.Exemption, ExemptionStartDate = s.Exemption_Start_Date, ExemptionEndDate = s.Exemption_End_Date, " & _
          "ExemptionReason = s.Exemption_Text, ExemptionDescription = s.Exemption_Reason, Ownership = s.Ownership, NECC2500 = s.NECC_2500, NECC3500 = s.NECC_3500, " & _
          "NECC5000 = s.NECC_5000, NECC10000 = s.NECC_10000, NECCStartDate = s.NECC_Start_Date, NECCEndDate = s.NECC_End_Date, SPSCertificate = s.SPS_Certificate, " & _
          "ProductionStatus850 = s.PS_850, ProductionStatus855 = s.PS_855, ProductionStatus856 = s.PS_856, ProductionStatus810C = s.PS810C, " & _
          "ProductionStatus810F = s.PS_810F, PlatformFI850 = s.P850_FI, PlatformFI855 = s.P855_FI, PlatformFI856 = s.P856_FI, PlatformFI810C = s.P810C_FI, " & _
          "PlatformFI810F = s.P810F_FI, ProductionDate850 = s.PDTS_850,ProductionDate855 = s.PDTS_855,ProductionDate856 = s.PDTS_856, ProductionDate810C = s.PDTS_810C, " & _
          "ProductionDate810F = s.PDTS_810F, ProductionPrepack = s.Prepack, ProductionCrossDock = s.Cross_Dock, ProductionMultiStyle = s.[M-style], " & _
          "ProductionProductAttribute = s.PCA, ProductionMasterPack = s.Master_Pack, ProductionCrossDockMix = s.[XD-Mix], ProductionStandAloneMix = s.[SA_Mix], " & _
          "ProductionLegalEntity = s.Legal_Entity, FirstASNDate = s.First_ASN, FirstINVDate = s.First_INV, SALeadDays = s.Lead_Days, CrossDockLeadDays = s.Cross_Dock_Lead_Days, " & _
          "CommunicationNotes = s.CNotes, ProductionNotes = s.Info," & _
          "QU1 = s.QU1, QU2 = s.QU2, ISA_ID1 = s.ISA_ID1, ISA_ID2 = s.ISA_ID2, GS_ID1 = s.GS_ID1, GS_ID2 = s.GS_ID2, VAN = s.VAN, VAN2 = s.VAN2, HYBRID = s.HYBRID " & _
       "FROM " & _
          "(SELECT EDI_Contact_Name,EDI_Contact_Phone,EDI_Contact_Email,Platform,Status,Migration_Status,Migration_Date,Tester,Testing_Start_Date,Last_Contact," & _
                  "Status_850,Status_856,Status_810C,Status_810F,Status_MH10_Label,Status_PTicket,Tnotes,TS_850,TS_855,TS_856,TS_810C,TS_810F,GLD_850,GLD_855," & _
                  "GLD_856,GLD_810C,GLD_810F,Kick_Off_Mtg,Exemption,Exemption_Start_Date,Exemption_End_Date,Exemption_Text,Exemption_Reason,Ownership," & _
                  "NECC_2500,NECC_3500,NECC_5000,NECC_10000,NECC_Start_Date,NECC_End_Date,SPS_Certificate,PS_850,PS_855,PS_856,PS_810C,PS_810F,P850_FI,P855_FI," & _
                  "P856_FI,P810C_FI,P810F_FI,PDTS_850,PDTS_855,PDTS_856,PDTS_810C,PDTS_810F,Prepack,Cross_Dock,[M-style],PCA,Master_Pack,[XD-Mix],[SA-Mix],Legal_Entity," & _
                  "First_ASN,First_INV,Lead_Days,Cross_Dock_Lead_Days,CNotes,Info,QU1,QU2,ISA_ID1,ISA_ID2,GS_ID1,GS_ID2,VAN,VAN2,HYBRID " & _
             "FROM tbleVendorRecord JOIN tbleVendorData ON tbleVendorRecord.VendorID = tbleVendorData.VendorNumber) s"

看看你的WHERE子句。我做了,我觉得很好。。。VendorNumber是正确的对我来说不正确。正如Access向您暗示的那样,VendorNumber背后的价值是什么?它似乎不是select语句中的列,也不是变量,也不是除VendorNumber之外的任何内容。如果不是来自您指定的列,引擎应该使用什么值进行比较?SQL和我都不知道答案,所以您会得到一个提示。如果表本身在查询中未被引用,则无法引用表中的字段。在SELECT中,tbleVendorData不显示在何处。在追加查询中,SELECT部分看不到INSERTINTO@Maldred-您需要的是UPDATE语句,而不是INSERT。您不需要将列别名为相同的名称,insert和select语句的列顺序负责映射。尝试复制粘贴此语句,我不得不在其中添加一些SQL=SQL&因为它说我有太多的行连续体;但是这给了我一个脚本错误。。。我会在上面的问题中贴一张照片是的,它很长。也许您可以删除所有放入tbleVendorData中的现有空记录,然后简单地执行一个更简单的INSERT语句。我想我做不到,因为变量名和顺序已经改变了,这应该不会有问题。只需指定SELECT语句,字段名的顺序与目标表相同。它们不必具有相同的字段名,但它们的顺序必须相同。