我正在編寫一個存儲過程來刪除一個項目。項目有很多關係。結果在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
會寫的SQL完成提交/回滾目標是什麼? –
@ JimS-Indy您的程序的邏輯沒有改變:我的回答只是添加一個輸出參數來通知SP調用者出錯了。因此,如果SP在您的原始版本中完成了提交/回滾目標,即使增加了一個輸出參數,它也會完成它,但我認爲確保它可行的唯一方法就是試用它。 – Andrea