
DECLARE @DefaultConstraint SYSNAME
DECLARE @DropStatement NVARCHAR(500)

IF EXISTS (SELECT * FROM dbo.sysobjects WITH (NOLOCK) 
           WHERE 
             id = object_id(N'[dbo].[CustomerVarious]') AND 
             OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  SET @DropStatement = 'DROP TABLE CustomerVarious'
  EXEC (@DropStatement) 
  --PRINT 'CustomerVarious dropped'
END

IF EXISTS
(
  SELECT SysColumns.Name 
  FROM SysObjects WITH (NOLOCK) 
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'CustomerStatus'
    WHERE SysObjects.Name = 'Customer'
)
BEGIN
  SET @DropStatement = 'ALTER TABLE Customer DROP COLUMN CustomerStatus'
  EXEC (@DropStatement)  
  --PRINT 'CustomerStatus dropped'
END

IF EXISTS
(
  SELECT SysColumns.Name 
  FROM SysObjects WITH (NOLOCK) 
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'Restaurant'
    WHERE SysObjects.Name = 'Customer'
)
BEGIN
  SET @DropStatement = 'ALTER TABLE Customer DROP COLUMN Restaurant'
  EXEC (@DropStatement) 
  --PRINT 'Restaurant dropped' 
END

IF EXISTS
(
  SELECT SysColumns.Name 
  FROM SysObjects WITH (NOLOCK) 
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'ProvisionAccount'
    WHERE SysObjects.Name = 'Customer'
)
BEGIN
  SET @DropStatement = 'ALTER TABLE Customer DROP COLUMN ProvisionAccount'
  EXEC (@DropStatement)  
  --PRINT 'ProvisionAccount dropped'
END

IF EXISTS
(
  SELECT SysColumns.Name 
  FROM SysObjects WITH (NOLOCK) 
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'NetCash'
    WHERE SysObjects.Name = 'Customer'
)
BEGIN
  SET @DropStatement = 'ALTER TABLE Customer DROP COLUMN NetCash'
  EXEC (@DropStatement)  
  --PRINT 'NetCash dropped'
END

IF EXISTS
(
  SELECT SysColumns.Name 
  FROM SysObjects WITH (NOLOCK) 
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'ProvisionBill'
    WHERE SysObjects.Name = 'Customer'
)
BEGIN
  SET @DropStatement = 'ALTER TABLE Customer DROP COLUMN ProvisionBill'
  EXEC (@DropStatement)  
  --PRINT 'ProvisionBill dropped'
END

SELECT @DefaultConstraint = SYSOBJECTS.NAME
FROM INFORMATION_SCHEMA.COLUMNS WITH (NOLOCK) JOIN SYSCOLUMNS WITH (NOLOCK) ON object_id(INFORMATION_SCHEMA.COLUMNS.table_name) = SYSCOLUMNS.id
  JOIN SYSOBJECTS WITH (NOLOCK) ON SYSOBJECTS.id = SYSCOLUMNS.cdefault
         AND COLUMN_DEFAULT IS NOT NULL AND INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME = SYSCOLUMNS.NAME
         AND INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = 'Customer' 
         AND INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME = 'ProvisionPostOnGuest'
		 AND INFORMATION_SCHEMA.COLUMNS.DATA_TYPE = 'BIT'

IF @DefaultConstraint <> 'Nope'
BEGIN
  SET @DropStatement = 'ALTER TABLE CUSTOMER DROP CONSTRAINT ' + @DefaultConstraint 
  EXEC (@DropStatement)
  --PRINT 'ProvisionPostOnGuest dropped'  
END

IF EXISTS
(
  SELECT SysColumns.Name 
  FROM SysObjects WITH (NOLOCK) 
    INNER JOIN SysColumns WITH (NOLOCK) ON SysColumns.Id = SysObjects.ID 
    AND SysColumns.Name = 'ProvisionPostOnGuest'
    WHERE SysObjects.Name = 'Customer'
)  
  ALTER TABLE Customer DROP COLUMN ProvisionPostOnGuest
