Board logo

標題: [發問] [發問] 有關於SQL不存在則insert存在則update [打印本頁]

作者: taiwan16699    時間: 2019-11-17 18:12     標題: [發問] 有關於SQL不存在則insert存在則update

請問各位大大有關於SQL不存在則insert存在則update的問題

我要將Excel的Table Update到Access資料庫,當資料存在時即Update不存在時即insert
因為我非專業,故上網查詢一般人的寫法,以下三段語法分別(Select, Update, Insert)都能執行,但我該如何整合,才能達到呢?
  1. Private baglan As Object, rs As Object

  2. Sub baglanti()
  3.     Set baglan = CreateObject("adodb.connection")
  4.     #If VBA7 And Win64 Then
  5.     baglan.Open "provider=microsoft.ace.oledb.12.0;data source=" & ThisWorkbook.Path & "\SKYEYEDatabase.mdb"
  6.     #Else
  7.     baglan.Open "provider=microsoft.jet.oledb.4.0;data source=" & ThisWorkbook.Path & "\SKYEYEDatabase.mdb"
  8.     #End If
  9. End Sub

  10. Sub FP_Database_Acess()
  11.     Sheets("Menu-FP").Visible = True
  12.     Sheets("Menu-FP").Select
  13.     Dim shFC As Worksheet
  14.     'Dim aArticle As String
  15.     Set shFC = Sheets("Menu-FP")
  16.     EndR = shFC.Range("A65536").End(xlUp).Row
  17.    

  18.     For R = 7 To EndR
  19.         CustomerID = shFC.Cells(R, 1)
  20.         Customer = "'" & shFC.Cells(R, 2) & "'"
  21.         SPECID = shFC.Cells(R, 3)
  22.         aArticle = "'" & shFC.Cells(R, 4) & "'"
  23.         MD = "'" & shFC.Cells(R, 5) & "'"
  24.         Qty = shFC.Cells(R, 6)
  25.         Area = "'" & shFC.Cells(R, 7) & "'"
  26.         Remark = "'" & shFC.Cells(R, 8) & "'"
  27.         PNL = "'" & shFC.Cells(R, 9) & "'"
  28.         Grade = "'" & shFC.Cells(R, 10) & "'"
  29.         Package = "'" & shFC.Cells(R, 11) & "'"
  30.         ICVID = shFC.Cells(R, 12)
  31.         Path = "'" & shFC.Cells(R, 13) & "'"
  32.         File = "'" & shFC.Cells(R, 14) & "'"
  33.         
  34.         Call baglanti
  35.         
  36.         Set rs1 = baglan.Execute("(SELECT CustomerID=" & CustomerID & ",Customer= " & Customer & ", SPECID=" & SPECID & ", MaterialDescription=" & MD & ",Qty=" & Qty & ",Area=" & Area & ",Remark=" & Remark & ", ProductNameLIMS=" & PNL & ",Grade=" & Grade & ", Package=" & Package & ",ICVID=" & ICVID & ",Path=" & Path & ",File=" & File & "  FROM [FC] WHERE Article= " & aArticle & ")")

  37.         Set rs2 = baglan.Execute("(UPDATE [FC] SET CustomerID=" & CustomerID & ",Customer= " & Customer & ", SPECID=" & SPECID & ", MaterialDescription=" & MD & ",Qty=" & Qty & ",Area=" & Area & ",Remark=" & Remark & ", ProductNameLIMS=" & PNL & ",Grade=" & Grade & ", Package=" & Package & ",ICVID=" & ICVID & ",Path=" & Path & ",File=" & File & " WHERE Article= " & aArticle & ")")

  38.         Set rs3 = baglan.Execute("INSERT INTO [FC] (CustomerID,Customer,SPECID,Article,MaterialDescription,Qty,Area,Remark,ProductNameLIMS,Grade,Package,ICVID,Path,File) Values (" & CustomerID & "," & Customer & "," & SPECID & "," & aArticle & "," & MD & "," & Qty & "," & Area & "," & Remark & "," & PNL & "," & Grade & "," & Package & "," & ICVID & "," & Path & "," & File & ")")
  39.         
  40.         Set baglan = Nothing: Set rs1 = Nothing: Set rs2 = Nothing: Set rs3 = Nothing:
  41.    
  42.     Next
  43.     MsgBox ("DONE")
  44. End Sub
複製代碼

作者: allenlin99    時間: 2019-11-25 21:56

供你參考,insert into 整表格作法,讓你有方向去思考。
https://reurl.cc/xDeqmZ

其他語法供你參考
1. 只新增不存在的
strCommandText = "INSERT INTO TABLE select a.* from [Excel 12.0;Database=" & ActiveWorkbook.FullName & "].[" & myTable & "$" & Sheets("TABLE ").Range("A1").CurrentRegion.Address(0, 0) & "] a " & _ "left join " & Up2Table & " b on a.憑單=b.憑單 where b.憑單 is null"

2. UPDATE
strCommandText = "UPDATE " & Up2Table & " a " & _",[Excel 12.0;Imex=0;Database=" & ThisWorkbook.FullName & ";].[" & myTable & "$" & Sheets("svrOrdMaster").Range("A1").CurrentRegion.Address(0, 0) & "] b " & _"SET a.憑單=b.憑單 " & _"WHERE a.憑單=b.憑單"




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)