Metadata Functions (Transact-SQL)

Metadata functions return information about the database schema, objects, and system configuration. These functions are useful for querying information about your SQL Server environment directly from T-SQL.

DB_ID()

Returns the database identification number.

Learn More →

DB_NAME()

Returns the database name for a given database ID.

Learn More →

OBJECT_DEFINITION()

Returns the definition of a database object.

Learn More →

OBJECT_ID()

Returns the object identification number of a database object.

Learn More →

OBJECT_NAME()

Returns the name of a database object.

Learn More →

SCHEMA_NAME()

Returns the schema name for a given schema ID.

Learn More →

TYPE_NAME()

Returns the name of a data type.

Learn More →

STATS_DATE()

Returns the last update statistics date for a table or view.

Learn More →

COL_LENGTH()

Returns the length of a column in bytes.

Learn More →

COL_NAME()

Returns the name of a column.

Learn More →

Examples

Here's an example of how to get the name of a database given its ID:

SELECT DB_NAME(1); -- Returns the name of the master database
SELECT DB_NAME(DB_ID('AdventureWorks2019')); -- Returns the ID and then the name of the AdventureWorks2019 database

And an example of retrieving the definition of a stored procedure:

SELECT OBJECT_DEFINITION(OBJECT_ID('usp_GetCustomerInfo')); -- Returns the T-SQL code for the stored procedure