2013-09-05 79 views
0

我正在編寫一個存儲過程來刪除一個項目。項目有很多關係。結果在9個刪除語句中。如果有人失敗(儘管他們不應該......)我想向調用者(MS Access vba程序)返回一個錯誤並回滾事務。從MS Access調用的SQL Server SP中返回錯誤

我覺得我非常接近......有人可以把我逼過來嗎?

create procedure dbo.cpas_DeleteProject 
@ProjectID INt = 0 
AS 
BEGIN TRY 
    BEGIN TRANSACTION 
     DELETE FROM tblWOTasks WHERE tblWOTasks.WorkOrderID IN (SELECT ID FROM tblWorkOrders WHERE [email protected]) 
     DELETE FROM tblELaborSpread WHERE tblELaborSpread.WorkOrderID IN (SELECT ID FROM tblWorkOrders WHERE [email protected]) 
     DELETE FROM tblWorkOrders WHERE tblWorkOrders.ProjectID IN (SELECT ID FROM tblWorkOrders WHERE [email protected]) 
     DELETE FROM tblCPTransactiON WHERE [email protected] 
     DELETE FROM tblCPJE WHERE tblcpje.jeid IN 
      (SELECT tblcpje.JEID FROM tblCPJE left joIN tblCPTransactiON as CR ON CR.CPTransID = tblCPJE.JECreditID 
          left joIN tblCPTransactiON as DR ON DR.CPTransID = tblCPJE.JEDebitID 
        WHERE DR.CPTransID is null AND cr.CPTransID is null)   
     DELETE FROM tblProjectTasks WHERE [email protected] 
     DELETE FROM xrefProjectMICAP WHERE [email protected] 
     DELETE FROM tblworkorders WHERE [email protected] 
     DELETE FROM tblprojects WHERE [email protected] 
     --Project Comments cascade delete.... 
    COMMIT TRANSACTION 
END TRY 

BEGIN CATCH 
      {somewhere here I need to post an error message so Access will see it...} 
    ROLLBACK TRANSACTION 
END CATCH 

回答

1

我不知道這樣做的正確方法,但這裏只是我的建議。你可以在提交事務之後添加選擇0作爲錯誤代碼回滾事務後,選擇1作爲錯誤代碼

只有當某件事實際發生故障並且它進入CATCH塊時,纔會返回1。在您的VBA進程中,您可以返回記錄集並檢查rs(「ErrorCode」)。如果它不是零(0),那麼你知道你出錯了,並且它落在了CATCH塊。

0

你可以使用一個輸出參數(例如@errorFlag)只有當錯誤發生時將其設置爲1,並留下其設置爲0,否則:

create procedure dbo.cpas_DeleteProject 
@ProjectID INt = 0, 
@errorFlag int OUTPUT 
AS 
set @errorFlag=0 
BEGIN TRY 
    BEGIN TRANSACTION 
     DELETE FROM tblWOTasks WHERE tblWOTasks.WorkOrderID IN (SELECT ID FROM tblWorkOrders WHERE [email protected]) 
     DELETE FROM tblELaborSpread WHERE tblELaborSpread.WorkOrderID IN (SELECT ID FROM tblWorkOrders WHERE [email protected]) 
     DELETE FROM tblWorkOrders WHERE tblWorkOrders.ProjectID IN (SELECT ID FROM tblWorkOrders WHERE [email protected]) 
     DELETE FROM tblCPTransactiON WHERE tblC[email protected] 
     DELETE FROM tblCPJE WHERE tblcpje.jeid IN 
      (SELECT tblcpje.JEID FROM tblCPJE left joIN tblCPTransactiON as CR ON CR.CPTransID = tblCPJE.JECreditID 
          left joIN tblCPTransactiON as DR ON DR.CPTransID = tblCPJE.JEDebitID 
        WHERE DR.CPTransID is null AND cr.CPTransID is null)   
     DELETE FROM tblProjectTasks WHERE [email protected] 
     DELETE FROM xrefProjectMICAP WHERE [email protected] 
     DELETE FROM tblworkorders WHERE [email protected] 
     DELETE FROM tblprojects WHERE [email protected] 
     --Project Comments cascade delete.... 
    COMMIT TRANSACTION 
END TRY 

BEGIN CATCH 
    ROLLBACK TRANSACTION 
    set @errorFlag=1 
END CATCH 
+0

會寫的SQL完成提交/回滾目標是什麼? –

+0

@ JimS-Indy您的程序的邏輯沒有改變:我的回答只是添加一個輸出參數來通知SP調用者出錯了。因此,如果SP在您的原始版本中完成了提交/回滾目標,即使增加了一個輸出參數,它也會完成它,但我認爲確保它可行的唯一方法就是試用它。 – Andrea