
IF  EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_MedicalPatientOverview]') AND type in (N'U'))
BEGIN
	TRUNCATE TABLE pal_MedicalPatientOverview
	DROP TABLE pal_MedicalPatientOverview
END

IF EXISTS(SELECT * FROM sys.columns 
		WHERE Name = N'userId'
		AND Object_ID = Object_ID(N'pal_MedicalPatientVisitLog'))
BEGIN
    EXEC sp_RENAME 'pal_MedicalPatientVisitLog.userId', 'surfaceItemId', 'COLUMN' 
END

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

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

IF NOT EXISTS(SELECT * FROM sys.columns 
		WHERE Name = N'customCode'
		AND Object_ID = Object_ID(N'pal_SurfaceLookup'))
BEGIN
	ALTER TABLE pal_SurfaceLookup
		ADD customCode VARCHAR(50) NULL;
END

IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_MedicalPatientPracticeExtra]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_MedicalPatientPracticeExtra] (
		[recId]           INT IDENTITY (1, 1) NOT NULL,
		[surfaceItemId]   INT NULL,
		[practiceExtraId] INT NULL,
		[isActive]        BIT NULL,
		CONSTRAINT [PK_pal_MedicalPatientPracticeExtra] PRIMARY KEY CLUSTERED ([recId] ASC)
	);
END

IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[pal_MedicalPracticeExtra]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[pal_MedicalPracticeExtra] (
		[recId]         INT           IDENTITY (1, 1) NOT NULL,
		[practiceExtra] VARCHAR (200) NULL,
		[isActive]      BIT           NULL,
		CONSTRAINT [PK_pal_MedicalPracticeExtra] PRIMARY KEY CLUSTERED ([recId] ASC)
	);
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_CalendarAccess]') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE [dbo].[sp_CalendarAccess]
END

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