How ORIGINAL_DB_NAME() Works in SQL Server

In SQL Server, you can use the ORIGINAL_DB_NAME() function to return the database name specified by the user in the database connection string.

This function is not to be confused with the DB_NAME() function, which returns either a specific database, or the current one.

When you first make a connection to SQL Server, you have the option of specifying the initial database. For example, when using a command line interface such as sqlcmd, you can use the -d parameter to specify the initial database. If you don’t use this parameter, your login’s default database will be the initial database.

Once you connect, you can switch to a different database, but your original database will always be the same. In other words, ORIGINAL_DB_NAME() will always return the same database throughout your session, even if you switch to a different database.

Continue reading

Use DB_ID() to Return the ID of a Database in SQL Server

In SQL Server, you can use the DB_ID() function to return the ID of the current database, or another specified database.

The way it works is, you pass the name of the database as an argument, and then the function will return the ID of that database. If you don’t pass a name it will return the ID of the current database.

Continue reading

Improved Script that Returns All Properties from SERVERPROPERTY() in SQL Server

Below is a (slightly) improved script that returns all properties from the SERVERPROPERTY() function in SQL Server.

This is similar to the quick script that I posted recently. The difference is that for some properties, the script below uses CASE statements to provide a short explanation of the configured value, rather than the raw value (which is often a “1” or “0”). The aforementioned quick script, on the other hand, simply outputs the raw data for all rows.

Continue reading

Quick Script that Returns All Properties from SERVERPROPERTY() in SQL Server 2017/2019

The following script returns all properties (and their respective values) from the SERVERPROPERTY() function in SQL Server 2017 and 2019.

The property names are returned in the first column, and their raw values are returned in the second column.

In total, 44 properties are returned by this script.

Continue reading

Use TYPEPROPERTY() to Return Information about a Data Type in SQL Server

In SQL Server, you can use the TYPEPROPERTY() function to return information about a data type.

You provide the name of the data type, as well as the property you want returned, and it returns information about that property for the given data type.

You can use TYPEPROPERTY() for system data types and user-defined data types.

Continue reading

Use TYPE_ID() to Get the ID of a Data Type in SQL Server

In SQL Server, you can use the TYPE_ID() function to return the ID of a data type, based on its name. This can be useful when querying a system view that stores a data type’s ID but not its name. It’s usually easier to remember the name. Not so easy to remember the ID.

You can use TYPE_ID() for system data types and user-defined data types.

Continue reading