Use APP_NAME() to Get the Application Name of the Current Session in SQL Server

In SQL Server, you can use the APP_NAME() function to get the application name for the current session. This assumes the application sets that name value.

You can use this function to distinguish between different applications, as a way to perform different actions for those applications.

Note that the client provides the application name, and so the result returned by this function simply reflects whatever name the client provides. For this reason, Microsoft advises that this function should not be used for security checks.

Continue reading

Use COLUMNPROPERTY() to Return Column or Parameter Information in SQL Server

In SQL Server, the COLUMNPROPERTY() function returns column or parameter information.

For example, you can use it to return information about a column in a table, a parameter for a stored procedure, etc

It accepts three arguments: the ID of the table or procedure, the applicable column or parameter, and the property you want information about.

Continue reading

Use DATABASEPROPERTYEX() to Return Database Settings in SQL Server

In SQL Server, the DATABASEPROPERTYEX() function returns the current setting of the specified database option or property for the specified database.

For example, you can use it to return the collation of a database, check the level of user access, etc.

It accepts two arguments: the database, and the property you want information about.

Continue reading

OBJECTPROPERTY() vs OBJECTPROPERTYEX() in SQL Server: What’s the Difference?

In SQL Server, you might’ve encountered the OBJECTPROPERTY() function, only to find out that there’s also a OBJECTPROPERTYEX() function that appears to do exactly the same thing.

What’s going on here? Why the need for two functions that do the same thing?

My understanding is that Microsoft chose to add OBJECTPROPERTYEX() to extend the functionality of OBJECTPROPERTY(), rather than to introduce changes to OBJECTPROPERTY() that would potentially break existing code on legacy systems.

So there are some differences between the two functions.

Continue reading

How OBJECTPROPERTYEX() Works in SQL Server

In SQL Server, the OBJECTPROPERTYEX() function returns information about schema-scoped objects in the current database.

This function does exactly the same thing as OBJECTPROPERTY(), except that it supports more properties, and the return value is different. The OBJECTPROPERTYEX() function returns a sql_variant type, whereas OBJECTPROPERTY() returns an int type.

Continue reading

How OBJECTPROPERTY() Works in SQL Server

In SQL Server, the OBJECTPROPERTY() function returns information about schema-scoped objects in the current database.

These schema-scoped objects are the ones you can see by querying the sys.objects system catalog view. It can’t be used for objects that are not schema-scoped.

You can use OBJECTPROPERTY() to check if an object is a table, view, stored procedure, etc. You can also use it to check if a table has a primary key, foreign key, foreign key reference, etc.

Continue reading

DB_NAME() vs ORIGINAL_DB_NAME() in SQL Server: What’s the Difference?

Two of the metadata functions available in SQL Server include DB_NAME() and ORIGINAL_DB_NAME(). Both functions are similar in that they return the name of a database. But they are also different. You definitely don’t want to confuse the two, as they serve different purposes.

In a nutshell, each function works as follows:

  • DB_NAME() returns the name of a specified database. If you don’t explicitly specify a database, it returns the current database.
  • ORIGINAL_DB_NAME() returns the database name specified by the user in the database connection string.

Continue reading

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