I created a script to rename tables that I copied into another database. However, when I run it it gives me an error saying that the renamed table is in use. this is the 2nd table that it renamed.
Error message "Error: The new name 'IPro\_ALERT\_1604220600' is already in use as a object name and would cause a duplicate that is not permitted."

I am not sure why it is giving this error and I have looked around this site and elsewhere and I think my code should work. Any ideas on what/why it is doing this?

Thanks in advance.

Code for renaming tables:

```
USE [II_to_IIRP]
BEGIN
SET NOCOUNT ON;

    DECLARE @table_name SYSNAME
    DECLARE @table_part SYSNAME

    DECLARE comp_cursor cursor FOR
    SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE = 'BASE TABLE'
    AND TABLE_NAME  LIKE '%_1604220600'  
    ORDER BY TABLE_NAME

OPEN COMP_CURSOR

    FETCH NEXT FROM COMP_CURSOR INTO @table_name
    SET @table_part = 'IPro_' + @table_name 
    WHILE (@@fetch_status = 0 and @@error = 0 )
BEGIN
    PRINT 'Changing table Name '+@table_name
    EXEC sp_rename @table_name, @table_part
    PRINT 'Done Changing table '+@table_name
    SELECT 'drop table dbo.'+@table_name where @table_name <> @table_part
    FETCH NEXT FROM COMP_CURSOR INTO @table_name
    PRINT 'Getting next table ' + @table_name
END
CLOSE COMP_CURSOR
DEALLOCATE COMP_CURSOR
END;

```