我将放置一段 VBA 代码,您必须将其添加到您的代码中。(确保您有这些引用 Microsoft ActiveX Data Objects 6.1 Library 和 Microsoft ActiveX Data Objects Recordset 6.1 Library )
这是代码:
Public Sub Routine()
On Error GoTo ErrorHandler
Dim Cnxn As ADODB.Connection
Dim strCnxn As String
Dim sqlStr0 As String
'Dim ...all SQL strings you need
'Dim ...all recordsets you need
'Open Connection and begin transaction
strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _
"Initial Catalog='[Your Data base here]';Integrated Security='SSPI';"
Set Cnxn = New ADODB.Connection
Cnxn.Open strCnxn
Cnxn.BeginTrans
'Sql String to create a TEMPORARY TABLE using YOUR ORIGIN TABLE as a TEMPLATE
sqlStr0 = "create temporary table TempTableA as select * from tableA where 1 = 2;"
Cnxn.Execute sqlStr0
'Put your code here to insert the 40 records into TempTableA
'Put your code here to move records from TempTableA to definitive TableB
'Do not worry about TempTableA anymore. It will be destroyed.
Cnxn.CommitTrans
Cnxn.Close
Set Cnxn = Nothing
Exit Sub
ErrorHandler:
' clean up
If Not Cnxn Is Nothing Then
If Cnxn.State = adStateOpen Then
Cnxn.RollbackTrans
Cnxn.Close
End If
Set Cnxn = Nothing
End If
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub
一种方法:
SELECT GET_LOCK('no-nonsense');
SELECT RELEASE_LOCK('no-nonsense');
我建议使用临时表而不是插入和截断。它们对其他会话不可见,因此您可以让多个会话创建相同的“TEMPTABLEA”、插入记录、删除记录,甚至销毁 TEMP TABLE,而不会干扰其他用户的工作。当您终止会话时,临时表会自动被销毁。
我将放置一段 VBA 代码,您必须将其添加到您的代码中。(确保您有这些引用 Microsoft ActiveX Data Objects 6.1 Library 和 Microsoft ActiveX Data Objects Recordset 6.1 Library )
这是代码:
祝你好运!!