我正在编写一个存储过程来删除一个项目。项目有很多关系。产生 9 个删除语句。如果任何一个失败(尽管他们不应该......)我想向调用者(一个 MS Access vba 程序)返回一个错误并回滚事务。
我想我已经很接近了……有人可以把我推过线吗?
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 ProjectID=@ProjectID)
DELETE FROM tblELaborSpread WHERE tblELaborSpread.WorkOrderID IN (SELECT ID FROM tblWorkOrders WHERE ProjectID=@ProjectID)
DELETE FROM tblWorkOrders WHERE tblWorkOrders.ProjectID IN (SELECT ID FROM tblWorkOrders WHERE ProjectID=@ProjectID)
DELETE FROM tblCPTransactiON WHERE tblCPTransactiON.CPProjectID=@ProjectID
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 tblProjectTasks.ProjectID=@ProjectID
DELETE FROM xrefProjectMICAP WHERE xrefProjectMICAP.ProjectID=@ProjectID
DELETE FROM tblworkorders WHERE tblWorkOrders.ProjectID=@ProjectID
DELETE FROM tblprojects WHERE tblProjects.ID=@ProjectID
--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