SharePoint 2010 Zero Site Collection Count After Attaching a Content Database

SharePoint 2010 zero site collection count after attaching a content database. There are various reasons for this, I am discussing one among that, 

Scenario:

My colleague needs to clone 15 site collections from source to destination on the same farm. One thing he found was all the 15 sites were under a dedicated content database. So he decided to take a backup of the content database and restore it in SQL with different name and attach it to the destination. He did the same way. At first step after restoring a duplicate copy of the content database in SQL, when he tried to add the content database to the destination web app he go the below error,

The attach operation cannot continue because another object in this farm already contains the same ID. Each object in a farm must have a unique ID. In order to proceed with the attach operation you must assign a new ID to this database. To attach this database with a new ID, use the Mount-SPContentDatabase command with the -AssignNewDatabaseId parameter. Note that if this new database and an existing database contain the same site collections, attaching this database will likely result in orphaned site collections due to conflicts between the two databases.

So he used Mount-SPContentDatabase command with the -AssignNewDatabaseId parameter to add the content database.

Mount-SPContentDatabase "DatabaseName" -DatabaseServer "DataBaseServer" -WebApplication http://sitename -AssignNewDatabaseId

Now the content database was added to the destination web app successfully, but the site collection count was 0.

He was wondering for long time on where he made the mistake. Later he thought there may be some orphans and used the Test-spcontentdatabase command to find the list of it.

Test-SpContentDatabase –name “Dtabase name” –webapplication Http://sitename

He got a site orphan error message in the list of orphans,

  • Category: SiteOrphan.

  • Error: True.

  • UpgradeBlocking: False.

  • Message: Database [Databasename] contains a site (Id = [DatabaseID], Url = [/]) whose id is already associated with a different database (Id = [DatabaseID], name = [Databasename]) in the site map. Consider deleting one of these sites which have conflicting ids.

  • Remedy: The orphaned sites could cause upgrade failures. Try detach and reattach the database which contains the orphaned sites. Restart upgrade if necessary.

Later he realized that he was doing database attach detach within the same farm where he should end with restoring a site collection with sameID where it exists in the source content database in the same farm.

This was the reason he ended up with 0 site collections after attaching a content database.