USE [EnVisage]
GO

/****** Object:  Table [dbo].[Resource]    Script Date: 9/22/2014 1:56:50 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

begin transaction

begin try
CREATE TABLE [dbo].[Resource](
	[Id] [uniqueidentifier] NOT NULL,
	[FirstName] [nvarchar](250) NOT NULL,
	[LastName] [nvarchar](250) NOT NULL,
	[IsActiveEmployee] [bit] NOT NULL,
 CONSTRAINT [PK_Resource] 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 [dbo].[Resource] ADD  CONSTRAINT [DF_Resource_IsActiveEmployee]  DEFAULT ((0)) FOR [IsActiveEmployee]

CREATE TABLE [dbo].[ResourceExpenditureCategories](
	[ResourceId] [uniqueidentifier] NOT NULL,
	[EpenditureCategoryId] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_ResourceExpenditureCategories] PRIMARY KEY CLUSTERED 
(
	[ResourceId] ASC,
	[EpenditureCategoryId] 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 [dbo].[ResourceExpenditureCategories]  WITH CHECK ADD  CONSTRAINT [FK_ResourceExpenditureCategories_Expenditure_Category] FOREIGN KEY([EpenditureCategoryId])
REFERENCES [dbo].[Expenditure_Category] ([Id])

ALTER TABLE [dbo].[ResourceExpenditureCategories] CHECK CONSTRAINT [FK_ResourceExpenditureCategories_Expenditure_Category]

ALTER TABLE [dbo].[ResourceExpenditureCategories]  WITH CHECK ADD  CONSTRAINT [FK_ResourceExpenditureCategories_Resource] FOREIGN KEY([ResourceId])
REFERENCES [dbo].[Resource] ([Id])

ALTER TABLE [dbo].[ResourceExpenditureCategories] CHECK CONSTRAINT [FK_ResourceExpenditureCategories_Resource]

CREATE TABLE [dbo].[ResourceTeams](
	[ResourceId] [uniqueidentifier] NOT NULL,
	[TeamId] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_ResourceTeams] PRIMARY KEY CLUSTERED 
(
	[ResourceId] ASC,
	[TeamId] 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 [dbo].[ResourceTeams]  WITH CHECK ADD  CONSTRAINT [FK_ResourceTeams_Resource] FOREIGN KEY([ResourceId])
REFERENCES [dbo].[Resource] ([Id])

ALTER TABLE [dbo].[ResourceTeams] CHECK CONSTRAINT [FK_ResourceTeams_Resource]

ALTER TABLE [dbo].[ResourceTeams]  WITH CHECK ADD  CONSTRAINT [FK_ResourceTeams_Team] FOREIGN KEY([TeamId])
REFERENCES [dbo].[Team] ([Id])

ALTER TABLE [dbo].[ResourceTeams] CHECK CONSTRAINT [FK_ResourceTeams_Team]

end try
begin catch
SELECT 
         ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_LINE () AS ErrorLine
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
end catch

IF @@TRANCOUNT > 0
    COMMIT TRANSACTION;

GO