所以我解决了我最初的问题。但是,现在我的数据实际上并没有被excel文件中的数据更新。相反,#temp表永远不会采用excel文件的值,而是保留输入的原始数据。如何确保数据来自excel文件?现在我输入原始表中的数据纯粹是为了保持结构。然后我想用临时表上的新数据替换它。然后用临时表数据替换表中的数据。
Dim cmd As Object
Set cmd = CreateObject("ADODB.Command")
Dim beforeSQL As String
With CN
.Execute " Select * Into #temp1 from dim.DepartmentLeader"
End With
Dim level As Long
level = CN.BeginTrans
cmd.CommandType = 1 ' adCmdText
' Dim rst As ADODB.Recordset
Dim rst As Object
Set rst = CreateObject("ADODB.Recordset")
With rst
Set .ActiveConnection = CN
.Source = "SELECT * FROM #temp1"
.CursorLocation = 3 ' adUseClient
.LockType = 4 ' adLockBatchOptimistic
.CursorType = 0 ' adOpenForwardOnly
.Open
' Column mappings
Dim tableFields(8) As Integer
Dim rangeFields(1000) As Integer
Dim exportFieldsCount As Integer
exportFieldsCount = 0
Dim col As Integer
Dim index As Integer
Dim lastRow As String
Dim copyRange As String
lastRow = LastRowInOneColumn()
For col = 0 To .Fields.Count - 1
index = Application.Match(.Fields.Item(col).Name, Range("A1:H249").Rows(1), 0)
If index > 0 Then
exportFieldsCount = exportFieldsCount + 1
tableFields(exportFieldsCount) = col
rangeFields(exportFieldsCount) = index
End If
Next
If exportFieldsCount = 0 Then
ExportRangeToSQL = 1
GoTo ConnectionEnd
End If
' Fast read of Excel range values to an array
' for further fast work with the array
Dim arr As Variant
arr = Range(Cells(1, 13).Value).Value
' The range data transfer to the Recordset
Dim row As Long
Dim rowCount As Long
rowCount = UBound(arr, 1)
Dim val As Variant
For row = 2 To rowCount
.AddNew
For col = 1 To exportFieldsCount
val = arr(row, rangeFields(col))
Debug.Print row
If IsEmpty(val) Then
Else
.Fields(tableFields(col)) = val
End If
Next
Next
.UpdateBatch
End With
With CN
.Execute "update dim.DepartmentLeader set " & _
"AUDescriptions = #temp1.AUDescriptions, " & _
"Dim.DepartmentLeader.Status = #temp1.Status, " & _
"dim.DepartmentLeader.Pillar = #temp1.Pillar," & _
"dim.DepartmentLeader.L1 = #temp1.L1, " & _
"dim.DepartmentLeader.L2 = #temp1.L2," & _
"dim.DepartmentLeader.L3 = #temp1.L3," & _
"dim.DepartmentLeader.L4 = #temp1.L4 " & _
"FROM dim.DepartmentLeader Inner Join #temp1 On dim.DepartmentLeader.DepartmentNumber= #temp1.DepartmentNumber " & _
"WHERE dim.DepartmentLeader.DepartmentNumber = #temp1.DepartmentNumber " & _
"IF @@ROWCOUNT = 0 " & _
"insert into dim.DepartmentLeader (dim.DepartmentLeader.DepartmentNumber, dim.DepartmentLeader.AUDescriptions, dim.DepartmentLeader.Status, dim.DepartmentLeader.Pillar,dim.DepartmentLeader.L1, dim.DepartmentLeader.L2,dim.DepartmentLeader.L3,dim.DepartmentLeader.L4)" & _
" SELECT * FROM #temp1 "
End With
rst.Close
Set rst = Nothing
ConnectionEnd:
CN.CommitTrans
答案 0 :(得分:0)
您正在使用带有UpdateBatch
光标的adOpenForwardOnly
。您应该只将UpdateBatch
与Keyset
或Static
光标一起使用。有关详细信息,请参阅此MSDN文章:https://msdn.microsoft.com/en-us/library/windows/desktop/ms675283(v=vs.85).aspx
With rst
Set .ActiveConnection = cn
.Source = "SELECT * FROM ##temp1"
.CursorLocation = 3 ' adUseClient
.LockType = 4 ' adLockBatchOptimistic
'Use a Keyset cursor in prep for using UpdateBatch
.CursorType = 1 ' adOpenKeyset
......
......
End With
或者,您可以考虑在循环中为每个单独的记录集使用rst.Update
。