PostgreSQL / VBA:事务块失败检查或进程

时间:2017-01-26 15:45:07

标签: vba postgresql excel-vba tsql excel

在事务阻止期间,如果其中一部分失败或被中断,处理此问题的最佳方法是什么?

我正在考虑中间查询(请参阅代码注释)以确认先前的查询是否成功。或者有更好的方法吗?

Sub SetDataProcessPriorities()

Dim cCon As New ADODB.Connection
Set cCon = Application.Run("Personal.xlsb!ConnectToPg")
Call Application.Run("Personal.xlsb!SendPgQueryOnly", "begin;", cCon)

Call Application.Run("Personal.xlsb!SendPgQueryOnly", "delete from data_proc_pri;", cCon)
    'intermediate select query here?                                                    
Call Application.Run("Personal.xlsb!SendPgQueryOnly", "insert into data_proc_pri                                              select * from data_proc_pri_store;", cCon)
    'intermediate select query here?                                                    
Call Application.Run("Personal.xlsb!SendPgQueryOnly", "commit;", cCon)
End Sub

我对VBA(对不起)或PostgreSQL(或两者)关注的解决方案持开放态度。

奖金问题:你怎么称呼这种类型的节目?基础设施规划?

如果您需要,可以使用个人工作簿代码:

Sub SendPgQueryOnly(sSql As String, Optional cCon As ADODB.Connection, Optional sOdbcName As String = "[omitted]")
sCaller = "SendPgQueryOnly"

    Dim cnDB As New ADODB.Connection

    Dim rsRecords As New ADODB.Recordset

    If IsMissing(cCon) Or cCon Is Nothing Then
        cnDB.Open sOdbcName
        rsRecords.CursorType = 2 'adOpenDynamic
        rsRecords.Open sSql, cnDB
    Else
        rsRecords.CursorType = 2 'adOpenDynamic
        rsRecords.Open sSql, cCon 'use the passed connection obj
    End If


cleanup:
    Set rsRecords = Nothing
    Set cnDB = Nothing
End Sub

Function ConnectToPg(Optional sOdbcName As String = "[omitted]") As ADODB.Connection
    sCaller = "ConnectToPg"

    Dim cnDB As New ADODB.Connection

    cnDB.Open sOdbcName
    Set ConnectToPg = cnDB
    End Function

1 个答案:

答案 0 :(得分:0)

对于事务控制,您可以在Connection对象上使用BeginTrans,CommitTrans和RollbackTrans方法

https://msdn.microsoft.com/en-us/library/ms680895(v=vs.85).aspx

虽然考虑到特定问题,我有一定程度的编程风格和宽容度,但我猜想可以使用一些Select查询来查询数据库,以确保数据库状态按预期更改;如果符合预期则提交,如果没有则再回滚。