USE [EnVisage]
GO

IF (EXISTS(SELECT * FROM sys.objects WHERE Name = N'FK_Team2Scenario_Scenario'))
BEGIN
	ALTER TABLE [dbo].[Team2Scenario] DROP CONSTRAINT [FK_Team2Scenario_Scenario]
END
GO

IF (EXISTS(SELECT * FROM sys.objects WHERE Name = N'FK_Team2Scenario_Team'))
BEGIN
	ALTER TABLE [dbo].[Team2Scenario] DROP CONSTRAINT [FK_Team2Scenario_Team]
END
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_DeleteScenario] (@aScenarioOID uniqueidentifier) AS
	BEGIN
		BEGIN TRANSACTION
		update dbo.Scenario set TemplateId = null where TemplateId = @aScenarioOID
		delete from dbo.TeamAllocation where ScenarioId = @aScenarioOID
		delete from dbo.CostSaving where ScenarioId = @aScenarioOID --SA. ENV-885
		delete from dbo.PeopleResourceAllocation where ScenarioId = @aScenarioOID --SA. ENV-885
		delete from dbo.Scenario2Group where ScenarioId = @aScenarioOID --SA. ENV-885
		delete from Note where ParentId = @aScenarioOID
		delete from Rate where ParentId = @aScenarioOID
		delete from ScenarioDetail where ScenarioDetail.ParentID = @aScenarioOID
		delete from ScenarioAccess where ScenarioAccess.ParentId = @aScenarioOID
		delete from Scenario where id = @aScenarioOID
		COMMIT TRANSACTION 
END
GO

