-- Some old MisTable contained nullable columns ReferenceNumber and FolioNumber
-- Remove these columns before recreating in pre.

if EXISTS
(
 SELECT SysColumns.Name 
  FROM SysObjects(NOLOCK) 
    INNER JOIN SysColumns(NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'ReferenceNumber'
    WHERE SysObjects.Name = 'MISTable' and syscolumns.isnullable = 1
)
begin
	DECLARE @ntable nvarchar(50)
	DECLARE @ncolumn nvarchar(50) 
	DECLARE @nconstraint nvarchar(50)
	SELECT @ntable = 'MISTable', @ncolumn = 'ReferenceNumber'
	SELECT @nconstraint = object_name(cdefault) from syscolumns
	where name = @ncolumn AND id = object_id(@ntable)
	exec(' alter table ' + @ntable + ' drop constraint ' + @nconstraint)
	SELECT @ntable = 'MISTable', @ncolumn = 'FolioNumber'
	SELECT @nconstraint = object_name(cdefault) from syscolumns
	where name = @ncolumn AND id = object_id(@ntable)
	exec(' alter table ' + @ntable + ' drop constraint ' + @nconstraint)


	ALTER TABLE MISTable DROP COLUMN ReferenceNumber
	ALTER TABLE MISTable DROP COLUMN FolioNumber
end
GO