
-- SQLAzureMWParseTSQL TSQL Output File:
-- Generated at: 3/1/2014 3:07:23 PM

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

	CREATE TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsLocation](
		[locationId] [int] IDENTITY(1,1) NOT NULL,
		[moduleId] [int] NOT NULL,
		[locationName] nvarchar(max) not null,
		[isArchived] bit not null,
		[parentId] int null
	 CONSTRAINT [PK_{objectQualifier}tblNaDevEventsLocation] PRIMARY KEY CLUSTERED 
	(
		[locationId] ASC
	)
	)
 end
GO

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

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

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

create procedure {databaseOwner}{objectQualifier}usp_nadev_Event_Location_Save_Location
(
 @locationId int,
 @moduleId int,
 @locationName nvarchar(max),
 @isArchived bit,
 @parentId int,
 @longitude nvarchar(255),
 @latitude nvarchar (255)
)

as

set nocount on
  
 if exists(select null from {databaseOwner}[{objectQualifier}tblNaDevEventsLocation] where locationId = @locationId)
	begin
		update {databaseOwner}[{objectQualifier}tblNaDevEventsLocation] set moduleId = @moduleId,
																			 locationName = @locationName,
																			 isArchived = @isArchived,
																			 parentId = @parentId,
																			 longitude = @longitude,
																			 latitude = @latitude
																		  										 
																			where locationId = @locationId
																			return @@identity
			end 
else
	insert into {databaseOwner}[{objectQualifier}tblNaDevEventsLocation] (moduleId,locationName,isArchived,parentId,longitude,latitude)

	values (@moduleId,@locationName,@isArchived,@parentId,@longitude,@latitude)
	
	SELECT SCOPE_IDENTITY()

GO

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

create procedure {databaseOwner}[{objectQualifier}usp_nadev_Event_Location_Get_All]
(
 @moduleId int
)

as

	select * from {databaseOwner}[{objectQualifier}tblNaDevEventsLocation] 
		where moduleId = @moduleId order by locationName

return

GO

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

create procedure {databaseOwner}[{objectQualifier}usp_nadev_Event_Location_Get]
(
 @locationId int
)

as

	select * from {databaseOwner}[{objectQualifier}tblNaDevEventsLocation] 
		where locationId = @locationId

return

GO

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

create procedure {databaseOwner}[{objectQualifier}usp_nadev_Event_Location_Delete]
(
 @locationId int
)

as

	delete from {databaseOwner}[{objectQualifier}tblNaDevEventsLocation] where locationId = @locationId

return

GO
