Create a unique database ID in Microsoft SQL Server

I work with Microsoft SQL Server everyday and have been developing scripts to monitor such things as database growth and the amount of free HD space on each of the systems.

The script I have that records HD space stores the DBID of each database next to each recording so I can track growth over time. “Sounds fine so far” you say. So did I…

When developing some code to report on the data collected I noticed that there were several databases where the sizes had changed after a period of having zero size.

Further investigation showed that MSSQL had reused IDs that belonged to databases I’d previously deleted. Doh! I needed a better way of differentiating between the databases I was monitoring. Continue reading