IF exists(SELECT * FROM sysobjects WHERE name = 'LD0SPDeleteActualization')
	DROP PROCEDURE LD0SPDeleteActualization
GO
CREATE PROCEDURE LD0SPDeleteActualization
 @ACTUALIZATIONID int,
 @PROCESSMETADATAID int
   
AS

BEGIN

BEGIN TRANSACTION DeleteActualization

BEGIN TRY
  
  DECLARE @SAVEDCOMPONENTID  INT; SET @SAVEDCOMPONENTID = 0;
  DECLARE @PROCESSSUMMARYID  INT; SET @PROCESSSUMMARYID = 0;
  DECLARE @CLIENTTASKSID  INT; SET @CLIENTTASKSID = 0;
  DECLARE @CASEWORKERHISTORYID  INT; SET @CASEWORKERHISTORYID = 0;
  DECLARE @SUCCESS DECIMAL(1,0); set @SUCCESS = 0;
  
  -- Delete dependent data first

  -- TABLE: LD0P0SAVEDCOMPONENTS
  select @SAVEDCOMPONENTID = ID from LD0P0SAVEDCOMPONENTS where PROCESSMETADATAID = @PROCESSMETADATAID;
  delete from LD0P0SAVEDCOMPONENTS where ID = @SAVEDCOMPONENTID;

  -- TABLE: LD0P0PROCESSSUMMARY
  select @PROCESSSUMMARYID = ID from LD0P0PROCESSSUMMARY where PROCESSMETADATAID = @PROCESSMETADATAID;
  delete from LD0P0PROCESSSUMMARY where ID = @PROCESSSUMMARYID;
  
  -- TABLE: LD0P0CLIENTTASKS
  select @CLIENTTASKSID = ID from LD0P0CLIENTTASKS where PROCESSMETADATAID = @PROCESSMETADATAID;
  delete from LD0P0CLIENTTASKS where ID = @CLIENTTASKSID;

  -- TABLE: LD0P0CASEWORKERHISTORY
  select @CASEWORKERHISTORYID = ID from LD0P0CASEWORKERHISTORY where PROCESSID = @PROCESSMETADATAID;
  delete from LD0P0CASEWORKERHISTORY where ID = @CASEWORKERHISTORYID;
   
  -- TABLE: LD0P0PROCESSMETADATA
  delete from LD0P0PROCESSMETADATA where ID = @PROCESSMETADATAID;

  --Delete Actualization
  delete from LD0P0H where AKTUALNR = @ACTUALIZATIONID;


  COMMIT TRANSACTION DeleteActualization;

  SET @SUCCESS = 1;

END TRY

BEGIN CATCH

  ROLLBACK TRANSACTION DeleteActualization;

  SET @SUCCESS = 0;

END CATCH

select @SUCCESS as 'SUCCESS';

END

GO
GRANT ALL ON LD0SPDeleteActualization TO LD0ROANV
GO