CREATE PROC pAutoAuditDrop (
   @SchemaName SYSNAME  = 'dbo',
   @TableName SYSNAME
) 
AS 
SET NoCount ON

DECLARE 
   @SQL NVARCHAR(max)

-- drop default constraints

If Exists (select * 
             from sys.objects o 
               join sys.schemas s on o.schema_id = s.schema_id   
             where o.name = @TableName + '_Created_df'
               and s.name = @SchemaName)
  BEGIN 
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] drop constraint ' + @TableName + '_Created_df'
    EXEC (@SQL)
  END

If Exists (select * 
             from sys.objects o 
               join sys.schemas s on o.schema_id = s.schema_id   
             where o.name = @TableName + '_CreatedBy_df'
               and s.name = @SchemaName)
  BEGIN 
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] drop constraint ' + @TableName + '_CreatedBy_df'
    EXEC (@SQL)
  END


If Exists (select * 
             from sys.objects o 
               join sys.schemas s on o.schema_id = s.schema_id   
             where o.name = @TableName + '_Modified_df'
               and s.name = @SchemaName)
  BEGIN 
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] drop constraint ' + @TableName + '_Modified_df'
    EXEC (@SQL)
  END

If Exists (select * 
             from sys.objects o 
               join sys.schemas s on o.schema_id = s.schema_id   
             where o.name = @TableName + '_ModifiedBy_df'
               and s.name = @SchemaName)
  BEGIN 
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] drop constraint ' + @TableName + '_ModifiedBy_df'
    EXEC (@SQL)
  END

If Exists (select * 
             from sys.objects o 
               join sys.schemas s on o.schema_id = s.schema_id   
             where o.name = @TableName + '_RowVersion_df'
               and s.name = @SchemaName)
  BEGIN 
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] drop constraint ' + @TableName + '_RowVersion_df'
    EXEC (@SQL)
  END

-- drop Created column 
IF exists (select *
			  from sys.tables t
				join sys.schemas s
				  on s.schema_id = t.schema_id
				join sys.columns c
				  on t.object_id = c.object_id
			  where  t.name = @TableName AND s.name = @SchemaName and c.name = 'Created')
  BEGIN
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] DROP COLUMN Created'
    EXEC (@SQL)
  END

IF exists (select *
			  from sys.tables t
				join sys.schemas s
				  on s.schema_id = t.schema_id
				join sys.columns c
				  on t.object_id = c.object_id
			  where  t.name = @TableName AND s.name = @SchemaName and c.name = 'CreatedBy')
  BEGIN
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] DROP COLUMN CreatedBy'
    EXEC (@SQL)
  END

-- drop Modified column 
IF exists( select *
			  from sys.tables t
				join sys.schemas s
				  on s.schema_id = t.schema_id
				join sys.columns c
				  on t.object_id = c.object_id
			  where  t.name = @TableName AND s.name = @SchemaName and c.name = 'Modified')
  BEGIN   
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] DROP COLUMN Modified'
    EXEC (@SQL)
  END

IF exists (select *
			  from sys.tables t
				join sys.schemas s
				  on s.schema_id = t.schema_id
				join sys.columns c
				  on t.object_id = c.object_id
			  where  t.name = @TableName AND s.name = @SchemaName and c.name = 'ModifiedBy')
  BEGIN
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] DROP COLUMN ModifiedBy'
    EXEC (@SQL)
  END

-- drop RowVersion column 
IF exists( select *
			  from sys.tables t
				join sys.schemas s
				  on s.schema_id = t.schema_id
				join sys.columns c
				  on t.object_id = c.object_id
			  where  t.name = @TableName AND s.name = @SchemaName and c.name = 'RowVersion')
  BEGIN   
    SET @SQL = 'ALTER TABLE [' + @SchemaName + '].[' + @TableName + '] DROP COLUMN RowVersion'
    EXEC (@SQL)
  END


-- drop existing insert trigger
SET @SQL = 'If EXISTS (Select * from sys.objects o join sys.schemas s on o.schema_id = s.schema_id  '
       + ' where s.name = ''' + @SchemaName + ''''
       + '   and o.name = ''' + @TableName + '_Audit_Insert' + ''' )'
       + ' DROP TRIGGER ' + @SchemaName + '.' + @TableName + '_Audit_Insert'
EXEC (@SQL)

-- drop existing update trigger
SET @SQL = 'If EXISTS (Select * from sys.objects o join sys.schemas s on o.schema_id = s.schema_id  '
       + ' where s.name = ''' + @SchemaName + ''''
       + '   and o.name = ''' + @TableName + '_Audit_Update' + ''' )'
       + ' DROP TRIGGER ' + @SchemaName + '.' + @TableName + '_Audit_Update'
EXEC (@SQL)

-- drop existing delete trigger
SET @SQL = 'If EXISTS (Select * from sys.objects o join sys.schemas s on o.schema_id = s.schema_id  '
       + ' where s.name = ''' + @SchemaName + ''''
       + '   and o.name = ''' + @TableName + '_Audit_Delete' + ''' )'
       + ' DROP TRIGGER ' + @SchemaName + '.' + @TableName + '_Audit_Delete'
EXEC (@SQL)

-- drop existing _deleted view
SET @SQL = 'If EXISTS (Select * from sys.objects o join sys.schemas s on o.schema_id = s.schema_id  '
       + ' where s.name = ''' + @SchemaName + ''''
       + '   and o.name = ''v' + @TableName + '_Deleted' + ''' )'
       + ' DROP VIEW ' + @SchemaName + '.v' + @TableName + '_Deleted'
EXEC (@SQL)

-- drop existing _RowHistory UDF
SET @SQL = 'If EXISTS (Select * from sys.objects o join sys.schemas s on o.schema_id = s.schema_id  '
       + ' where s.name = ''' + @SchemaName + ''''
       + '   and o.name = ''' + @TableName + '_RowHistory' + ''' )'
       + ' DROP FUNCTION ' + @SchemaName + '.' + @TableName + '_RowHistory'
EXEC (@SQL)


go