USE [EnVisage]
GO

/****** Object:  StoredProcedure [dbo].[sp_DeleteProject]    Script Date: 2/27/2015 1:15:45 PM ******/
DROP PROCEDURE [dbo].[sp_DeleteProject]
GO

/****** Object:  StoredProcedure [dbo].[sp_DeleteProject]    Script Date: 2/27/2015 1:15:45 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[sp_DeleteProject] (@id uniqueidentifier) AS
	BEGIN
	
		BEGIN TRANSACTION

		select Id into #projectId2Delete from Project where Project.ParentProjectId = @id or Project.Id = @id
		select Id into #scenarioId2Delete from Scenario where Scenario.ParentId in (select Id from #projectId2Delete)

		delete from History where EntityId in (select id from #scenarioId2Delete)
		delete from Note where ParentId in (select id from #scenarioId2Delete)
		delete from Rate where ParentId in (select id from #scenarioId2Delete)
		delete from ScenarioDetail where ScenarioDetail.ParentID in (select id from #scenarioId2Delete)
		delete from Scenario2Group where ScenarioId in (select id from #scenarioId2Delete)
		delete from PeopleResourceAllocation where ScenarioId in (select id from #scenarioId2Delete)
		delete from Team2Scenario where ScenarioId in (select id from #scenarioId2Delete)
		delete from CostSaving where ScenarioId in (select id from #scenarioId2Delete)
		delete from Scenario where id in (select id from #scenarioId2Delete)
		delete from Team2Project where ProjectId in (select Id from #projectId2Delete)
		delete from Contact2Project where ShowId in (select Id from #projectId2Delete)
		delete from ProjectAccess where ProjectId in (select Id from #projectId2Delete)
		delete from Project where id in (select Id from #projectId2Delete)
		
		COMMIT TRANSACTION 

 	END
GO


