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

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

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

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

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

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

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

alter procedure {databaseOwner}{objectQualifier}usp_nadev_EventAttributes_InsertUpdateAttribute
(
 @attributeId int,
 @eventId int,
 @attributeName nvarchar(255),
 @attributeValue ntext,
 @SortOrder int,
 @DefaultSelected bit
)

as

set nocount on
  
 if exists(select null from {databaseOwner}[{objectQualifier}tblNaDevEventAttributes] where attributeId = @attributeId)
	begin
		update {databaseOwner}[{objectQualifier}tblNaDevEventAttributes] set attributeName = @attributeName,
																	  attributeValue = @attributeValue,
																	  DefaultSelected = @DefaultSelected,
																	  SortOrder = @SortOrder							 
									 
																	  where attributeId = @attributeId
																	  return @@identity
			end 
else
	insert into {databaseOwner}[{objectQualifier}tblNaDevEventAttributes] (eventId, attributeName, attributeValue,SortOrder,DefaultSelected)

	values (@eventId, @attributeName, @attributeValue,@SortOrder,@DefaultSelected)
	
	SELECT SCOPE_IDENTITY()

GO

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

create procedure {databaseOwner}[{objectQualifier}usp_nadev_EventAttributes_GetAllAttributeValues]
(
 @attributeName nvarchar(255)
)

as

	select  DISTINCT CAST([attributeValue] AS NVARCHAR(4000)) as attValue,defaultselected,sortorder from {databaseOwner}[{objectQualifier}tblNaDevEventAttributes] as a
	inner join {databaseOwner}[{objectQualifier}tblNaDevEvents] as e
	on a.eventid = e.eventid
		where attributeName = @attributeName and e.isactive =1 and e.isdeleted = 0
		order by sortorder, attValue

return

GO