標題:
[發問]
[發問] 有關於SQL不存在則insert存在則update
[打印本頁]
作者:
taiwan16699
時間:
2019-11-17 18:12
標題:
[發問] 有關於SQL不存在則insert存在則update
請問各位大大有關於SQL不存在則insert存在則update的問題
我要將Excel的Table Update到Access資料庫,當資料存在時即Update不存在時即insert
因為我非專業,故上網查詢一般人的寫法,以下三段語法分別(Select, Update, Insert)都能執行,但我該如何整合,才能達到呢?
Private baglan As Object, rs As Object
Sub baglanti()
Set baglan = CreateObject("adodb.connection")
#If VBA7 And Win64 Then
baglan.Open "provider=microsoft.ace.oledb.12.0;data source=" & ThisWorkbook.Path & "\SKYEYEDatabase.mdb"
#Else
baglan.Open "provider=microsoft.jet.oledb.4.0;data source=" & ThisWorkbook.Path & "\SKYEYEDatabase.mdb"
#End If
End Sub
Sub FP_Database_Acess()
Sheets("Menu-FP").Visible = True
Sheets("Menu-FP").Select
Dim shFC As Worksheet
'Dim aArticle As String
Set shFC = Sheets("Menu-FP")
EndR = shFC.Range("A65536").End(xlUp).Row
For R = 7 To EndR
CustomerID = shFC.Cells(R, 1)
Customer = "'" & shFC.Cells(R, 2) & "'"
SPECID = shFC.Cells(R, 3)
aArticle = "'" & shFC.Cells(R, 4) & "'"
MD = "'" & shFC.Cells(R, 5) & "'"
Qty = shFC.Cells(R, 6)
Area = "'" & shFC.Cells(R, 7) & "'"
Remark = "'" & shFC.Cells(R, 8) & "'"
PNL = "'" & shFC.Cells(R, 9) & "'"
Grade = "'" & shFC.Cells(R, 10) & "'"
Package = "'" & shFC.Cells(R, 11) & "'"
ICVID = shFC.Cells(R, 12)
Path = "'" & shFC.Cells(R, 13) & "'"
File = "'" & shFC.Cells(R, 14) & "'"
Call baglanti
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 & ")")
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 & ")")
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 & ")")
Set baglan = Nothing: Set rs1 = Nothing: Set rs2 = Nothing: Set rs3 = Nothing:
Next
MsgBox ("DONE")
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/)