USE [EnVisage]
GO

if exists (select 1 from sys.all_objects where name like 'sp_DeleteProject')
begin
DROP PROCEDURE [dbo].[sp_DeleteProject]
end
GO

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

		select Id into #scenarioId2Delete from Scenario where Scenario.ParentId = @id
		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 Scenario where id in (select id from #scenarioId2Delete)
		delete from Team2Project where ProjectId = @id
		delete from Contact2Project where ShowId = @id
		delete from ProjectAccess where ProjectId = @id
		delete from Project where id = @id
		
		COMMIT TRANSACTION 

 	END




GO


