IF exists(SELECT * FROM sysobjects WHERE name = 'LD0SPDeleteInvestigation')
    DROP PROCEDURE LD0SPDeleteInvestigation
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE LD0SPDeleteInvestigation
 @INVESTIGATIONID int,
 @PROCESSMETADATAID int
   
AS

BEGIN

  DECLARE @SUCCESS DECIMAL(1,0); set @SUCCESS = 0;

	  -- Delete dependent data first

	  -- TABLE: LD0P0SAVEDCOMPONENTS
	  
	  delete from LD0P0SAVEDCOMPONENTS where PROCESSMETADATAID = @PROCESSMETADATAID;

	  -- TABLE: LD0P0PROCESSSUMMARY
	  delete from LD0P0PROCESSSUMMARY where PROCESSMETADATAID = @PROCESSMETADATAID;
  
	  -- TABLE: LD0P0CLIENTTASKS
	  delete from LD0P0CLIENTTASKS where PROCESSMETADATAID = @PROCESSMETADATAID;

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

	  --Delete Investigation
	  delete from LD0P0C where AEVERKNR=@INVESTIGATIONID

      SET @SUCCESS = 1;

select @SUCCESS as 'SUCCESS';

END

GRANT ALL ON LD0SPDeleteInvestigation TO LD0ROANV 

GO