#0157–SQL Server 2012–Deprecated features-DATABASEPROPERTY replaced by DATABASEPROPERTYEX


As I document the the database engine features that have been deprecated  in SQL Server 2012, one of the most important ones has to be the deprecation of DATABASEPROPERTY. I am yet to see a production code that does not use this function.

For those who came in late, DATABASEPROPERTY is a function that returns the named database property value for the specified database and property name. Here’s an example that uses the DATABASEPROPERTY function to see if the ANSI NULLS property is ON or OFF by default (runs on SQL Server 2008/R2).

SELECT DATABASEPROPERTY('AdventureWorks2008','IsAnsiNullDefault') AS IsAnsiNullDefault

/* Result Set
IsAnsiNullDefault
-----------------
0
*/

This function was quite old and did not cover some of the newly introduced properties like the comparison style for a collation. Also, the return type of the current function – DATABASEPROPERTY was an integer, which means that the function could not return a character or other data types.

Microsoft SQL Server 2005 therefore shipped with a new, replacement function – DATABASEPROPERTYEX. While the syntax is the same as the function it replaces, the return types are different. The extended, DATABASEPROPERTYEX now returns a sql_variant and therefore has the capability to return a string. We can therefore do the following which was not possible with the previous function.

SELECT DATABASEPROPERTYEX('AdventureWorks2012','Collation') AS Collation,
       DATABASEPROPERTYEX('AdventureWorks2012','Status') AS Status

/* Result Set
Collation                       Status
------------------------------  -------
SQL_Latin1_General_CP1_CI_AS    ONLINE
*/

I have already started replacing instances of the old DATABASEPROPERTY with DATABASEPROPERTYEX. Have you?

Reference:

Until we meet next time,

Be courteous. Drive responsibly.

Advertisements

2 thoughts on “#0157–SQL Server 2012–Deprecated features-DATABASEPROPERTY replaced by DATABASEPROPERTYEX

Let me know what you think about this post by leaving your feedback here!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s