2017-10-19 49 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