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

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

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

IF NOT EXISTS (
    SELECT *
      FROM sys.all_columns c
      JOIN sys.tables t on t.object_id = c.object_id
      JOIN sys.schemas s on s.schema_id = t.schema_id
      JOIN sys.default_constraints d on c.default_object_id = d.object_id
    WHERE t.name = 'pal_SurfaceField'
      AND c.name = 'gridSequence'
      AND s.name = 'dbo')
BEGIN 
	ALTER TABLE dbo.pal_SurfaceField ADD CONSTRAINT
		DF_pal_SurfaceField_gridSequence DEFAULT 99 FOR gridSequence

	ALTER TABLE dbo.pal_SurfaceField SET (LOCK_ESCALATION = TABLE)

	UPDATE	F
		SET	gridSequence = 99
	--SELECT	*
	FROM	pal_SurfaceField F
	WHERE	F.gridSequence IS NULL
END 

