if not exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}[{objectQualifier}tblNaDevEventsRecurringEventDate]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
 begin

	CREATE TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsRecurringEventDate](
		[Id] [int] IDENTITY(1,1) NOT NULL,
		[EventId] [int] NOT NULL,
		[StartDate] [datetime] not null,
		[EndDate] [datetime] not null,
		[LocationId] [int] null
	 CONSTRAINT [PK_{objectQualifier}tblNaDevEventsRecurringEventDate] PRIMARY KEY CLUSTERED 
	(
		[Id] ASC
	)
	)
 end
GO

if not exists (select column_name from INFORMATION_SCHEMA.columns where table_name = '{objectQualifier}tblNaDevEventPriceTier' and column_name = 'ReduceAll')
 begin
   alter table {databaseOwner}[{objectQualifier}tblNaDevEventPriceTier] add ReduceAll bit not null default 0
 end
GO

alter procedure {databaseOwner}{objectQualifier}usp_nadev_Event_Price_Tier_Save
(
 @priceId int,
 @eventId int,
 @TierName nvarchar(255),
 @costPerPlace money,
 @dnnRoleId int,
 @sortOrder int,
 @additionalAttendeeCost money,
 @QtyRequired int,
 @ReduceAll bit
)

as

set nocount on
  
 if exists(select null from {databaseOwner}[{objectQualifier}tblNaDevEventPriceTier] where priceId = @priceId)
	begin
		update {databaseOwner}[{objectQualifier}tblNaDevEventPriceTier] set eventId = @eventId,
																			TierName = @TierName,
																			costPerPlace = @costPerPlace,
																			dnnRoleId = @dnnRoleId,
																			sortOrder = @sortOrder,
																			additionalAttendeeCost = @additionalAttendeeCost,
																			QtyRequired = @QtyRequired,
																			ReduceAll = @ReduceAll
																																					  										 
																			where priceId = @priceId
																			return @@identity
			end 
else
	insert into {databaseOwner}[{objectQualifier}tblNaDevEventPriceTier] (eventId,TierName,costPerPlace,dnnRoleId,sortOrder,additionalAttendeeCost,QtyRequired,ReduceAll)

	values (@eventId,@TierName,@costPerPlace,@dnnRoleId,@sortOrder,@additionalAttendeeCost,@QtyRequired,@ReduceAll)
	
	SELECT SCOPE_IDENTITY()

GO

if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}[{objectQualifier}usp_naDevEventsGetCategory]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure {databaseOwner}{objectQualifier}usp_naDevEventsGetCategory
GO

create procedure {databaseOwner}{objectQualifier}usp_naDevEventsGetCategory
(
 @categoryId int
)

as

set nocount on
  
	select * from {databaseOwner}[{objectQualifier}tblNaDevEventCategories] 
	where categoryId = @categoryId

GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'{databaseOwner}[{objectQualifier}tblNaDevEventCategories]') AND name = N'{objectQualifier}nadevEvents_IDX_Category_ModuleId')
DROP INDEX [{objectQualifier}nadevEvents_IDX_Category_ModuleId] ON {databaseOwner}[{objectQualifier}tblNaDevEventCategories]
GO

CREATE NONCLUSTERED INDEX [{objectQualifier}nadevEvents_IDX_Category_ModuleId]
ON {databaseOwner}[{objectQualifier}tblNaDevEventCategories] ([moduleId])
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'{databaseOwner}[{objectQualifier}tblNaDevEventsLocation]') AND name = N'{objectQualifier}nadevEvents_IDX_Location_ModuleId')
DROP INDEX [{objectQualifier}nadevEvents_IDX_Location_ModuleId] ON {databaseOwner}[{objectQualifier}tblNaDevEventsLocation]
GO

CREATE NONCLUSTERED INDEX [{objectQualifier}nadevEvents_IDX_Location_ModuleId]
ON {databaseOwner}[{objectQualifier}tblNaDevEventsLocation] ([moduleId])
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'{databaseOwner}[{objectQualifier}tblNaDevEvents]') AND name = N'{objectQualifier}nadevEvents_IDX_Events_ModuleId')
DROP INDEX [{objectQualifier}nadevEvents_IDX_Events_ModuleId] ON {databaseOwner}[{objectQualifier}tblNaDevEvents]
GO

CREATE NONCLUSTERED INDEX [{objectQualifier}nadevEvents_IDX_Events_ModuleId]
ON {databaseOwner}[{objectQualifier}tblNaDevEvents] ([portalId])
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'{databaseOwner}[{objectQualifier}tblNaDevEventAttributes]') AND name = N'{objectQualifier}nadevEvents_IDX_EventAttributes_EventId')
DROP INDEX [{objectQualifier}nadevEvents_IDX_EventAttributes_EventId] ON {databaseOwner}[{objectQualifier}tblNaDevEventAttributes]
GO

CREATE NONCLUSTERED INDEX [{objectQualifier}nadevEvents_IDX_EventAttributes_EventId]
ON {databaseOwner}[{objectQualifier}tblNaDevEventAttributes] ([eventId])
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'{databaseOwner}[{objectQualifier}tblNaDevEventsUsersSignedUpToEvent]') AND name = N'{objectQualifier}nadevEvents_IDX_Booking_EventId')
DROP INDEX [{objectQualifier}nadevEvents_IDX_Booking_EventId] ON {databaseOwner}[{objectQualifier}tblNaDevEventsUsersSignedUpToEvent]
GO

CREATE NONCLUSTERED INDEX [{objectQualifier}nadevEvents_IDX_Booking_EventId]
ON {databaseOwner}[{objectQualifier}tblNaDevEventsUsersSignedUpToEvent] ([eventId])
GO