在此过程中,用户传递一个项目 ID。我希望能够传递不确定数量的项目 ID,以便我可以将 where 子句从“ProjectID=@projectID”(例如)更改为“ProjectID IN (@ProjectID)”。
这样,我可以通过一次调用数据库来删除十几个项目,而不是重复调用数据库。
有没有一个好的策略来做到这一点?我正在从 Access 调用 SP....
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 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
ROLLBACK TRANSACTION
set @errorFlag=1
END CATCH