2017-10-19 117 views
0

我有两个接近相同的表,唯一的区别是较新的有不同的字段名称。基本上,旧桌子已经过时了,需要重新工作;我试图提取所有字段,其中VendorNumber = Vendor_ID所有被复制到的字段保证为空。SQL从旧表中插入新表

下面的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 " 

我使用的MS Access中VBA并试图运行上面的脚本,我得到的“Enter参数值“供应商编号任何想法,为什么这是?

enter image description here

编辑:

enter image description here

+0

看看你的'WHERE'子句。 –

+0

我做了,它看起来对我来说很好...供应商编号是正确的 – Maldred

+0

看起来对我来说不正确。正如Access暗示给你的,VendorNumber背后的价值是什么?它看起来并不是你的select语句中的一列,或者是一个变量,或者除VendorNumber之外的任何东西。如果引擎不是来自您指定的列,引擎将使用什么值来比较它们? SQL(也不是我)知道答案,所以你会得到一个提示。 –

回答

2

由于目的地表已经加载了以及其要在其中存储记录的供应商的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" 
+0

尝试复制粘贴这个,我不得不添加一些SQL = SQL&在那里,因为它说我有太多的线延续;然而,这是给我一个脚本错误...我会在上面的问题张贴一张图片 – Maldred

+0

是的,这是很长的。也许你可以删除所有存放在tbleVendorData中的空记录,然后简单地执行一条简单的'INSERT'语句。 – STLDeveloper

+0

我不认为我可以,因为变量名称和顺序已更改 – Maldred

-1

你需要指定哪一列从旧表将被插入到新表列,你也需要把VALUES在您的查询中:

SQL =“INSERT INTO tbleVendorData(EDIContact,EDIPhone ........)”& _ “VALUES( SELECT EDI_Contact_Name 如EDIContact,EDI_Contact_Phone 如EDIPhone ....” & _ “FROM tbleVendorRecord” & _ “WHERE tbleVendorRecord.Vendor_ID = VendorNumber

+1

您不需要将colums别名为相同的名称,insert和select语句的列顺序会负责映射。 – DataWriter