SQL Server | SQL Server Transact-SQL
Programming language used to interact with SQL Server databases
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Tell me all i need to know about metadata in MSSQL databases
Programming language used to interact with SQL Server databases
In SQL Server, metadata is “data about data”: information that describes databases, objects, result sets, security, and configuration. It is used for schema validation, code generation, ETL, diagnostics, and security.
Key areas of metadata in SQL Server:
TYPE_ID(type_name) returns the ID of a data type.TYPE_NAME(type_id) returns the name of a data type from its ID.TYPEPROPERTY(type_name, 'PropertyName') returns properties such as Precision, Scale, AllowsNull, etc.COL_NAME(object_id, column_id) returns the column name for a given column ID.COL_LENGTH('schema.table', 'column') returns the length of a column in bytes.COLUMNPROPERTY(object_id, 'column_name', 'PropertyName') returns column properties such as IsIdentity, IsComputed, AllowsNull, and more.INDEX_COL('schema.table', index_id, key_ordinal) returns the name of an indexed column.INDEXKEY_PROPERTY(object_id, index_id, key_ordinal, 'PropertyName') returns index key properties (for example, sort order).INDEXPROPERTY(object_id, 'index_name', 'PropertyName') returns index properties such as whether an index is clustered, unique, or disabled.STATS_DATE(object_id, index_id) returns the last update date of statistics for a table or index.SERVERPROPERTY('PropertyName') returns instance-level properties such as edition, collation, product level.DB_ID('database_name') and DB_NAME(database_id) map between database names and IDs.DATABASEPROPERTYEX('database_name', 'PropertyName') returns database-level properties (for example, collation, status).ORIGINAL_DB_NAME() returns the original database name before a restore sequence.APP_NAME() returns the application name for the current session.DATABASE_PRINCIPAL_ID('principal_name') returns the ID of a database security principal.VERSION() returns the version string for Azure Synapse Analytics and Analytics Platform System (PDW).OBJECT_ID('schema.object_name') returns the ID for a schema-scoped object.OBJECT_NAME(object_id) returns the object name from its ID.OBJECT_SCHEMA_NAME(object_id) returns the schema name of an object.SCHEMA_ID('schema_name') and SCHEMA_NAME(schema_id) map between schema names and IDs.PARSENAME('server.database.schema.object', part) returns a specific part of a multipart object name.@@PROCID returns the ID of the currently executing stored procedure.OBJECT_DEFINITION(object_id) returns the definition (source text) of programmable objects.OBJECTPROPERTY(object_id, 'PropertyName') and OBJECTPROPERTYEX(object_id, 'PropertyName') return object properties (for example, whether it is a table, view, function, or has specific behaviors).SQLNumResultCols returns the number of columns in the result set.SQLDescribeCol returns basic column metadata (name, data type, length, etc.).SQLColAttribute returns additional attributes (precision, scale, nullability, etc.).sys.sensitivity_classifications returns classified columns and their information types, labels, and ranks. SELECT
schema_name(O.schema_id) AS schema_name,
O.name AS table_name,
C.name AS column_name,
information_type, label, rank, rank_desc
FROM sys.sensitivity_classifications sc
JOIN sys.objects O
ON sc.major_id = O.object_id
JOIN sys.columns C
ON sc.major_id = C.object_id
AND sc.minor_id = C.column_id;
sys_information_type_name, sys_sensitivity_label_name) and accessed via sys.extended_properties.VIEW ANY SENSITIVITY CLASSIFICATION.ALTER ANY SENSITIVITY CLASSIFICATION (implied by ALTER on the database or CONTROL SERVER).sp_help procedures.OBJECTPROPERTYEX may return NULL.sp_help procedures may return partial results or NULL.VIEW DEFINITION, or in SQL Server 2022 and later, VIEW SECURITY DEFINITION or VIEW PERFORMANCE DEFINITION at object, database, or server scope.Set<column name> for large data types (varchar(max), nvarchar(max), varbinary(max)), and generic operations (ExecuteNonQuery, ExecuteReader, ExecuteScalar).DBCC CHECKDB or DBCC CHECKCATALOG are used to diagnose and confirm corruption.References: