Sql-server – SQL Server: No Guid of a database

powershellsql server

I built a powershell script for collecting database information with the help of the class "Microsoft.SqlServer.Management.Smo.Server". My script
run into an error because one database has no DatabaseGuid. How could that happen?
I thought every database has a database guid?!
This database is in state "offline". I set an other database to state "offline" but the database has still a guid.

I thought each database reaceives a persistent guid, or?!


Best Answer

From this post: Obtaining a unique database identifier for SQL Server 2005 and later it seems that

probably SMO reads the database_guid value from sys.database_recovery_status:

Used to relate all the database files of a database together. All files must have this GUID in their header page for the database to start as expected. Only one database should ever have this GUID, but duplicates can be created by copying and attaching databases. RESTORE always generates a new GUID when you restore a database that does not yet exist.

And if you open the documentation sys.database_recovery_status (Transact-SQL) you'll find that

NULL= Database is offline, or the database will not start.

I tried to put some my databases offline and to query database_guid from sys.database_recovery_status and it is NULL while it's not NULL when bring these databases online again.