IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_SurfaceDateType]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_SurfaceDateType](
		[recId] [int] IDENTITY(1,1) NOT NULL,
		[surfaceDateType] [varchar](50) NULL,
		[isActive] [bit] NULL,
	 CONSTRAINT [PK_pal_SurfaceDateType] PRIMARY KEY CLUSTERED 
	(
		[recId] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY]

END



IF ((SELECT COUNT(*) FROM pal_SurfaceDateType WHERE recId = 1) = 0)
BEGIN
	SET IDENTITY_INSERT [dbo].[pal_SurfaceDateType] ON 
	INSERT [dbo].[pal_SurfaceDateType] ([recId], [surfaceDateType], [isActive]) VALUES (1, N'Calendar Date', 1)
	SET IDENTITY_INSERT [dbo].[pal_SurfaceDateType] OFF
END


IF ((SELECT COUNT(*) FROM pal_SurfaceDateType WHERE recId = 2) = 0)
BEGIN
	SET IDENTITY_INSERT [dbo].[pal_SurfaceDateType] ON 
	INSERT [dbo].[pal_SurfaceDateType] ([recId], [surfaceDateType], [isActive]) VALUES (2, N'Month / Year', 1)
	SET IDENTITY_INSERT [dbo].[pal_SurfaceDateType] OFF
END



IF NOT EXISTS(
    SELECT *
    FROM sys.columns 
    WHERE Name      = N'surfaceDateTypeId'
      AND Object_ID = Object_ID(N'pal_SurfaceField'))
BEGIN
    ALTER TABLE pal_SurfaceField
	ADD surfaceDateTypeId INT; 
END





--recreate surface procedures
IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'[dbo].[sp_GetSurfaceData]') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE [dbo].[sp_GetSurfaceData]
END

IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'[dbo].[sp_GetChildSurfaceData]') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE [dbo].[sp_GetChildSurfaceData]
END

IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'[dbo].[sp_GetSurfaceDataAll]') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE [dbo].[sp_GetSurfaceDataAll]
END

IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'[dbo].[sp_GetSurfaceItemData]') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE [dbo].[sp_GetSurfaceItemData]
END

IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'[dbo].[sp_GetSurfaceItemGridData]') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE [dbo].[sp_GetSurfaceItemGridData]
END