SET @dbname = DATABASE(); SET @tablename = "poisons"; SET @targetname = "positions"; SET @exists = ( SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = @dbname AND table_name = @tablename ); SET @target_exists = ( SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = @dbname AND table_name = @targetname ); SET @stmt = IF(@exists > 0 AND @target_exists = 0, CONCAT('RENAME TABLE ', @tablename, ' TO ', @targetname), 'SELECT 1' ); PREPARE stmt FROM @stmt; EXECUTE stmt; DEALLOCATE PREPARE stmt;