USE [EnVisage] GO IF (NOT EXISTS (SELECT * FROM sys.tables where name='PeopleResource2Team')) begin CREATE TABLE PeopleResource2Team( [Id] [uniqueidentifier] NOT NULL, [PeopleResourceId] [uniqueidentifier] NOT NULL, [TeamId] [uniqueidentifier] NOT NULL, [StartDate] [date] NOT NULL, [EndDate] [date] NULL, [Allocation] [smallint] NOT NULL, [DateCreated] [datetime] NOT NULL, CONSTRAINT [PK_PeopleResource2Team] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] ALTER TABLE PeopleResource2Team ADD CONSTRAINT [DF_PeopleResource2Team_Id] DEFAULT (newid()) FOR [Id] ALTER TABLE PeopleResource2Team ADD CONSTRAINT [DF_PeopleResource2Team_Allocation] DEFAULT ((100)) FOR [Allocation] ALTER TABLE PeopleResource2Team ADD CONSTRAINT [DF_PeopleResource2Team_DateCreated] DEFAULT (getdate()) FOR [DateCreated] ALTER TABLE PeopleResource2Team WITH CHECK ADD CONSTRAINT [FK_PeopleResource2Team_PeopleResource] FOREIGN KEY([PeopleResourceId]) REFERENCES PeopleResource ([Id]) ON UPDATE CASCADE ALTER TABLE PeopleResource2Team CHECK CONSTRAINT [FK_PeopleResource2Team_PeopleResource] ALTER TABLE PeopleResource2Team WITH CHECK ADD CONSTRAINT [FK_PeopleResource2Team_Team] FOREIGN KEY([TeamId]) REFERENCES Team ([Id]) ON UPDATE CASCADE ALTER TABLE PeopleResource2Team CHECK CONSTRAINT [FK_PeopleResource2Team_Team] CREATE NONCLUSTERED INDEX [IX_PeopleResource2Team1] ON [dbo].[PeopleResource2Team] ( [PeopleResourceId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] CREATE NONCLUSTERED INDEX [IX_PeopleResource2Team2] ON [dbo].[PeopleResource2Team] ( [TeamId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] CREATE NONCLUSTERED INDEX [IX_PeopleResource2Team3] ON [dbo].[PeopleResource2Team] ( [StartDate] ASC, [EndDate] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] end GO ALTER PROCEDURE sp_DeletePeopleResource (@id uniqueidentifier) AS BEGIN begin transaction delete from Holiday2PeopleResource where ResourceId = @id delete from NonProjectTimeAllocation where PeopleResourceId = @id delete from PeopleResourceVacation where PeopleResourceId = @id delete from PeopleResourceAllocation where PeopleResourceId = @id delete from Skill2Resource where ResourceId = @id delete from PeopleResource2Team where PeopleResourceId = @id delete from PeopleResource where Id = @id commit transaction END GO IF (NOT EXISTS(SELECT * FROM PeopleResource2Team)) BEGIN INSERT INTO PeopleResource2Team SELECT NEWID(), A.Id, A.TeamId, A.StartDate, A.EndDate, 100, A.StartDate FROM PeopleResource A WHERE EXISTS(SELECT B.* FROM Team B WHERE B.Id = A.TeamId) END GO