In this article, we explore a SQL function to extract SQL database metadata using the DATABASEPROPERTYEX function.
Introduction
We might want to retrieve the database property value. You can connect to SQL Instance using SSMS and get these configurations from the property window. You might not want to use GUI in case you need to check the property for multiple databases. It is possible but time-consuming. You cannot export the data in an excel format as well, and it again causes issues to write down values manually:
SQL Server provides built-in function DATABASEPROPERTY and DATABASEPROPERTYEX() to retrieve the information. It is essential to know the difference in these functions before proceeding further.
SQL Server 2012 introduced a new metadata function DATABASEPROPERTYEX. An earlier version of it, DATABASEPROPERTY, is still available but mainly for the backward compatibility. The main issue with DATABASEPROPERTY is that it returns only INT data type in the output. We get sql_variant data type output in the new function DATABASEPROPERTYEX.
Once we write this function in SSMS, you get information about its argument and return values.
It requires two arguments:
- Database Name: Specify the database name for which we want to retrieve the information
- Property Name: Specify the property name to get its configured value in the database specified in the first argument
Let’s explore this built-in function using examples.
Example 1: Get database collation
SQL Server collation is a set of characters and encoding rules. You might have a specific collation for a database depending upon the requirement.
Argument: Collation
1 |
Select DATABASEPROPERTYEX('AdventureWorks','Collation') as DBCollation |
In this case, the [AdventureWorks] database has a collation SQL_Latin1_General_CP1_CI_AS.
If we do not specify a database name in the first argument, it does not give any error message. You get NULL in the output.
Example 2: Check database status using DATABASEPROPERTYEX() function
We can have different statuses of a SQL database, as specified below:
- Online: It shows that the database is available for queries
- Offline: Sometimes, we do not want database available for users, but it should exist in the SQL instance. In this case, we can set the database to offline status
- Restoring: We can see restoring status if a database is being restored from the backup, or we have not performed the recovery on a database
- Suspect: In case of any issues with the database such as Primary or secondary data file corruption, you can see the database in the suspect mode
- Recovering: Once the database is performing a recovery process, you can see the database in recovery status. You might see this message in case you restart SQL Server while any active transactions are running
You can go through an article Understanding different SQL Server database states for more details.
Argument: Status
1 |
Select DATABASEPROPERTYEX('AdventureWorks','Status') as DBStatus |
As shown here, the database [AdventureWorks] database is in ONLINE status.
Example 3: Check database user access using DATABASEPROPERTYEX() function
Usually, a database remains in a multi-user mode. In this mode, many users can connect to the database and execute queries.
We can also set a database in a single user mode. In this mode, the only user with db_owner, dbcreator, or sysadmin can connect to a database, but only a single database connection is allowed.
Apart from these modes, we can set a database in the RESTRICTED_USER mode. In this mode, only users with db_owner, dbcreator, and sysadmin roles can connect to the database.
Argument: UserAccess
1 |
Select DATABASEPROPERTYEX('AdventureWorks','UserAccess') as [DBUserAccess] |
We have a database [AdventureWorks] in the multi-user mode.
Example 4: Check Auto Close property status of a SQL database using DATABASEPROPERTYEX() function
We can set a database to close automatically using the AUTO_CLOSE property set to true. In this case, the database shuts down and frees system resources once the last user exits.
For this example, let’s turn on AUTO_CLOSE property on my test database [Showroom] using the below script.
1 2 3 |
USE [master] GO ALTER DATABASE [Showroom] SET AUTO_CLOSE ON |
Now, we check the value for AUTO_CLOSE using the argument IsAutoClose in the [Showroom] and [AdventureWorks] databases.
1 2 |
Select DATABASEPROPERTYEX('Showroom','IsAutoClose') as [Auto_Close] Select DATABASEPROPERTYEX('Master','IsAutoClose') as [Auto_Close] |
In the below screenshot, we verify that the [Showroom] database is set to auto close whereas [AdventureWorks] database does not have this property enabled:
Example 5: Check Auto-Create and Auto-update Statistics property status of a SQL database using DATABASEPROPERTYEX() function
SQL Server query optimizer creates the statistics automatically based on the query predicates. We can see statistics starting from _WA_sys name.
You can refer to the SQL Server Statistics and how to perform Update Statistics in SQL article for SQL Server statistics.
Argument: IsAutoCreateStatistics
1 |
Select DATABASEPROPERTYEX('Showroom','IsAutoCreateStatistics') as [Auto Create Statistics] |
Argument: IsAutoUpdateStatistics
We can control the query optimizer behavior to update SQL Server statistics automatically based on a predefined threshold. You should always set this property enabled unless you have a specific reason to disable auto-update statistics.
1 |
Select DATABASEPROPERTYEX('Showroom','IsAutoUpdateStatistics') as [Auto Update Statistics] |
In the output, value 1 shows that auto-update statistic is turned on for [Showroom] database:
Example 6: Check whether the database is a schema and statistics only copy of a SQL database
SQL Server 2014 SP2 onwards, we can create a blank database with the schema-and statistics-only copy of a user database using DBCC CLONEDATABASE. We can use DATABASEPROPERTYEX to check whether it is a clone database or not.
1 |
Select DATABASEPROPERTYEX('AdventureWorks','IsClone') as [DB Clone] |
In an earlier example, we set [Showroom] database to auto-close once the last user exits. If we check the clone property for this database, it returns NULL output because It cannot access the database to check its property.
Argument: IsClone
1 |
Select DATABASEPROPERTYEX('Showroom','IsClone') as [DB Clone] |
Example 7: Check last date and time of the consistency check for a SQL database
We should perform regular consistency checks of all databases in SQL instance using DBCC CHECKDB. It ensures the database is consistent and does not have any corruption. We can check the timestamp of the last successful database consistency check.
Argument: LastGoodCheckDbTime
1 2 |
Select DATABASEPROPERTYEX('SQLShack','LastGoodCheckDbTime') as [Last Successful DBCC CHECKDB] |
In my demo environment, I do not have performed DBCC CHECKDB on the master database. If we have not performed a DBCC CHECKDB on a database, it returns default output 1900-01-01 00:00:00.000
1 2 |
Select DATABASEPROPERTYEX('Master','LastGoodCheckDbTime') as [Last Successful DBCC CHECKDB] |
Example 8: Check database recovery model for a SQL database
SQL Server provides Full, Bulk-logged, and Simple recovery model. It determines the kind of database backups and the ability to recover data in case of any issues.
Argument: Recovery
In the below query, we check the recovery model for three databases – Master, SQLShack & Javatest. We see different recovery model set for all databases in a SQL instance.
You can refer to Understanding SQL Server database recovery models to explore recovery models.
1 2 3 |
Select DATABASEPROPERTYEX('Master','Recovery') as [Master DB Recovery model], DATABASEPROPERTYEX('SQLShack','Recovery') as [SQLShack DB Recovery model], DATABASEPROPERTYEX('Javatest','Recovery') as [Javatest DB Recovery model] |
Example 9: Get values for different properties of all SQL databases using DATABASEPROPERTYEX function
We can go through the Microsoft docs for all arguments supported by the DATABASEPROPERTYEX function. In this example, we want to retrieve the configurations for all SQL databases available in the SQL instance. I found a useful script in Microsoft TechNet for it. I modified this script to include SQL Server 2019 version as well and attached to this article.
This script uses a Case statement in SQL Server. You can refer to the article CASE statement in SQL to learn it. It also gets the database name for the first argument using the sys.databases system view:
Download the DATABASEPROPERTYEX.sql file hereIn the output, we can see different properties returned for all SQL databases using the DATABASEPROPERTYEX function:
Conclusion
In this article, we explored useful DATABASEPROPERTYEX function to check configuration values for SQL databases. It comes as a handy and useful script where you need to check values for all SQL databases available in the instance. We cannot use the GUI method in this case, as it would take a longer time. You can use the DATABASEPROPERTYEX function to check the values, export it to CSV, Excel format, and store it for your records.
- Understanding PostgreSQL SUBSTRING function - September 21, 2024
- How to install PostgreSQL on Ubuntu - July 13, 2023
- How to use the CROSSTAB function in PostgreSQL - February 17, 2023