USE [envisage]
GO

/****** Object:  StoredProcedure [dbo].[sp_DeleteProject]    Script Date: 28.08.2014 14:21:24 ******/
if exists (select * from sys.all_objects where name like 'sp_DeleteProject')
DROP PROCEDURE [dbo].[sp_DeleteProject]
GO

/****** Object:  StoredProcedure [dbo].[sp_DeleteProject]    Script Date: 28.08.2014 14:21:24 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
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 Scenario_Detail where Scenario_Detail.ParentID in (select id from #scenarioId2Delete)
		delete from Scenario_Snapshot where ParentID in (select id from #scenarioId2Delete)
		delete from Scenario_Wide where ScenarioId in (select id from #scenarioId2Delete)
		delete from ScenarioAccess where ScenarioAccess.ParentId in (select id from #scenarioId2Delete)
		delete from ScenarioCln where id in (select id from #scenarioId2Delete)
		delete from Scenario where id in (select id from #scenarioId2Delete)
		delete from Project where id = @id
		
		COMMIT TRANSACTION 

 	END

GO


