﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__admin_removeconflicttables]
	@entitytype nvarchar(255) 
AS
BEGIN
	SET NOCOUNT ON;

    DECLARE @name nvarchar(255);
	IF RIGHT(@entitytype,1) <> 's'
		SET @entitytype = @entitytype + 's';

	DECLARE db_cursor CURSOR FOR 
		select [name] from sys.objects
		where LEFT(name,2) = 't_'
		order by create_date asc;

	OPEN db_cursor  
	FETCH NEXT FROM db_cursor INTO @name ;

	WHILE @@FETCH_STATUS = 0  
	BEGIN  
		  PRINT( @name);

		  IF LEFT(@name, 2) = 't_' and @name like ('%[_mfr__]%')
		  BEGIN
			  DECLARE @sql nvarchar(2000) = 'DROP TABLE [dbo].[' + @name + N'];';
			  EXECUTE sp_executesql @sql, N'@name nvarchar(255)', @name;
			  PRINT( @name + '  -->  dropped');
		  END
	  
		  FETCH NEXT FROM db_cursor INTO @name 
	END 

	CLOSE db_cursor  
	DEALLOCATE db_cursor 
END