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

	CREATE TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsSignUpQuestionTemplate](
		[TemplateId] [int] IDENTITY(1,1) NOT NULL,
		[ModuleId] [int] NOT NULL,
		[TemplateName] [nvarchar] (max) not null
	 CONSTRAINT [PK_{objectQualifier}tblNaDevEventsSignUpQuestionTemplate] PRIMARY KEY CLUSTERED 
	(
		[TemplateId] ASC
	)
	)
 end
GO

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

alter procedure {databaseOwner}{objectQualifier}usp_insertEventSignUpQuestion
(
 @eventId int,
 @questionName nvarchar(255),
 @controlType nvarchar(50),
 @isRequired bit,
 @sortOrder int,
 @AskQuestionToId int,
 @DefaultSelectOption bit,
 @TemplateId int
)

as

set nocount on
  
	insert into {databaseOwner}[{objectQualifier}tblNaDevEventSignUpQuestion] (eventId,questionName,controlType,isRequired,sortOrder,AskQuestionToId,DefaultSelectOption,TemplateId)

	values (@eventId,@questionName,@controlType,@isRequired,@sortOrder,@AskQuestionToId,@DefaultSelectOption,@TemplateId)
	
	SELECT SCOPE_IDENTITY()
			
	return

GO

alter procedure {databaseOwner}{objectQualifier}usp_updateEventSignUpQuestion
(
 @signUpQuestionId int,
 @eventId int,
 @questionName nvarchar(255),
 @controlType nvarchar(50),
 @isRequired bit,
 @sortOrder int,
 @AskQuestionToId int,
 @DefaultSelectOption bit,
 @TemplateId int
)

as

set nocount on
  
	update {databaseOwner}[{objectQualifier}tblNaDevEventSignUpQuestion] 
		set eventId =@eventId,
			questionName = @questionName,
			controlType = @controlType,
			isrequired = @isRequired,
			sortorder = @sortOrder,
			AskQuestionToId =@AskQuestionToId,
			DefaultSelectOption = @DefaultSelectOption,
			TemplateId = @TemplateId

			where signUpQuestionid = @signupquestionid

GO

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

create procedure {databaseOwner}{objectQualifier}usp_NaDevEvent_GetAllTemplateSignUpQuestions
(
@templateId int
)
as

set nocount on

select * from {databaseOwner}[{objectQualifier}tblNaDevEventSignUpQuestion]
where TemplateId = @templateId
order by sortOrder asc

GO