Retrieving SQL Server Metadata with Help of T-SQL

This article contains a list of scripts that mine the metadata in the various system functions, stored procedures, tables, and catalog views. Metadata queries are really helpful in discovering information for a given database schema. You can copy all the T-SQL scripts that are listed in this article and use for your own purposes. However, please do some tests before apply to a production database.

1 Server Information
1.1 Basic Server Information
1.2 Linked Servers
1.3 Get List of Databases from SQL Server
1.4 Last Backup
1.5 Active User Connections

2 Database Information
2.1 Database File Location
2.2 Tables
2.2.1 The Number of Records in a Table
2.2.2 Use the Clustered Index to Getting Row Counts
2.2.3 Finding Heaps (tables without clustered index)
2.2.4 Table Activity
2.3 Views
2.4 Synonyms
2.5 Stored Procedures
2.6 Functions
2.7 Triggers
2.8 Check Constraints

3 Drill Down Into the Data Model
3.1 Columns
3.1.1 Column Defaults
3.1.2 Computed Columns
3.1.3 Identity Columns
3.2 Keys and Indexes
3.2.1 Foreign Keys
3.2.2 Missing Indexes that support Foreign Keys
3.3 Object Dependencies
3.3.1 Query the System Catalog Views
3.3.2 Using a Common Table Expression

1. Server Information

1.1 Basic Server Information

The @@ functions provide basic server information.

To get the information about how long has your server been running since the last SQL Server startup?

The tempdb system database is recreated every time the server restarts.

1.2 Linked Servers

A linked server enables the SQL Server Database Engine to execute commands against OLE DB data sources outside of the instance of SQL Server. The following query checks whether your server linked to other servers.

Linked servers offer the following advantages:

  • The ability to access data from outside of SQL Server.
  • The ability to issue distributed queries, updates, commands, and transactions on heterogeneous data sources across the enterprise.
  • The ability to address diverse data sources similarly.

1.3 Get list of databases from SQL Server

There are several ways to get the list of all server databases.

If the caller of sys.databases is not the owner of the database and the database is not master or tempdb, the minimum permissions required to see the corresponding row are ALTER ANY DATABASE or VIEW ANY DATABASE server-level permission, or CREATE DATABASE permission in the master database. The database to which the caller is connected can always be viewed in sys.databases.

1.4 Last Backup

The following query returns the information about the most recent backup.

Next query provides you a location of the latest backup.

If you use SQL Server Management Studio, you can right-click a database, and then select Properties. The last backup date should be the first field in the dialog.

1.5 Active User Connections

The following query is legal only for SQL Server 2012 and higher. In the previous editions, the database id column was absent in DMV sys.dm_exec_sessions.

To find out which DATABASE is being accessed by users, you can use sp_who.

2 Database Information

If you need to get information about the objects in each of our databases, you can use various catalog views and Dynamic management Views. The sys.objects system table is one of the key tables for gathering information about the objects.

Below is the list of objects types on which we can filter (see also the sys.objects documentation on Microsoft’s MSDN website).

AF = Aggregate function (CLR)
P = SQL Stored Procedure
TA = Assembly (CLR) DML trigger
C = CHECK constraint
PC = Assembly (CLR) stored procedure
TF = SQL table-valued-function
D = DEFAULT (constraint or stand-alone)
PG = Plan guide
TR = SQL DML trigger
F = FOREIGN KEY constraint
PK = PRIMARY KEY constraint
TT = Table type
FN = SQL scalar function
R = Rule (old-style, stand-alone)
U = Table (user-defined)
FS = Assembly (CLR) scalar-function
RF = Replication-filter-procedure
UQ = UNIQUE constraint
FT = Assembly (CLR) table-valued function
S = System base table
V = View
IF = SQL inline table-valued function
SN = Synonym
X = Extended stored procedure
IT = Internal table
SQ = Service queue

You can use the OBJECTPROPERTY metadata function and sys.tables and sys.views to get the objects info that makes up our database schemas.

2.1 Database File Location

You can use the following queries to get a physical location of the main DB file (*.mdf) and the log file (*.ldf)

sys.database_files contains a row per file of a database as stored in the database itself.

2.2 Tables

Compared to GUI, scripts provide you with more flexibility while getting information about tables. INFORMATION_SCHEMA tables are based on ANSI standard, however, these tables do not provide an information about objects that are not the part of the standard (e.g. triggers and extended properties). You can use Catalog Views that return information that is used by the SQL Server Database Engine. It is recommended to use catalog views because they are the most general interface to the catalog metadata and provide the most efficient way to obtain, transform, and present customized forms of this information.

All user-available catalog metadata is exposed through catalog views.

2.2.1 The Number of Records in a Table

In SSMS, you can right click a required table and then click Table Properties. The Table Properties window provides an information about the number of records in a table. But what if you need to get this information for hundreds of tables? One way is to use SELECT COUNT(*) FROM TABLENAME, for every single table. Another way is to use T-SQL to generate a set of statements to return the row count for each table in the current database.

In case the previous query doesn’t work, try to add a schema name before tables.

sp_msForEachTable

sp_msforeachtable is non-documented Microsoft function.
The function loops through all the tables in a database executing a query, and replacing ‘?’ with each table name. sp_msforeachdb is a similar database function.
Note that these functions do not handle, some special characters in object names. For instance, the dash “-” symbol in the table name will lead to fail.

2.2.2 Use the Clustered Index to Getting Row Counts

All previous methods use COUNT(*). If a table contains 500K records, this method works quite slow. The fastest way to get row counts is to get the record counts from the clustered index or heap partition.

Microsoft states that the record count updates on indexes may not always match the record counts of the table. It happens due to a delay in the index counts. In most cases they are exactly the same or close and will be the same shortly.

2.2.3 Finding Heaps (tables without clustered index)

A heap is a table without a clustered index. One or more nonclustered indexes can be created on tables stored as a heap. Data is stored in the heap without specifying an order. Usually, data is initially stored in the order in which is the rows are inserted into the table, but the Database Engine can move data around in the heap to store the rows efficiently; so the data order cannot be predicted. To guarantee the order of rows returned from a heap, you must use the ORDER BY clause. To specify the order for storage of the rows, create a clustered index on the table so that the table is not a heap.

When to use a heap:
If a table is a heap and does not have any nonclustered indexes, then the entire table must be examined (a table scan) to find any row. This can be acceptable when the table is tiny, such as a list of the 12 regional offices of a company.

Do not use a heap when:
The data is frequently returned in a sorted order. A clustered index on the sorting column could avoid the sorting operation. Also, do not use a heap when the data is frequently grouped together. Data must be sorted before it is grouped, and a clustered index on the sorting column could avoid the sorting operation.

2.2.4 Table Activity

While doing some optimizations, it is important to understand what tables have the most reads and writes. Remember that the statistics from Dynamic Management Views are deleted every time SQL Server restarts.

Another version of this query uses a cursor to combine the information for all Tables and for all databases on the server. Although cursors have slow performance, navigating multiple databases is a good use for one.

2.3 Views

You can use SELECT DISTINCT in the view definition to make the view READ ONLY.
A view is only updateable in the case when each row in the view maps identically to a single row in the underlying table. A view that fails these criteria, such as any view built on more than one table, or that uses grouping, aggregations, and calculations in its definition will be read only.

2.4 Synonyms

2.5 Stored Procedures

You can use the catalog views to find out which stored procedures exist, what activity they perform, and which tables they reference.

With a simple addition to the WHERE clause of the stored procedure details query, you can view, for example, only those stored procedures that perform inserts.

Simply modify the WHERE clause as required to investigate stored procedures that do updates (LIKE ‘%update%’), deletes (LIKE ‘%delete%’), or reference a particular table (LIKE ‘%tablename%’).

2.6 Functions

2.7 Triggers

DML triggers is a special type of stored procedure that automatically takes effect when a data manipulation language (DML) event takes place that affects the table or view defined in the trigger. DML events include INSERT, UPDATE, or DELETE statements. DML triggers can be used to enforce business rules and data integrity, query other tables, and include complex Transact-SQL statements. The trigger and the statement that fires it are treated as a single transaction, which can be rolled back from within the trigger. If a severe error is detected (for example, insufficient disk space), the entire transaction automatically rolls back.

DDL triggers fire in response to a variety of Data Definition Language (DDL) events. These events primarily correspond to Transact-SQL statements that start with the keywords CREATE, ALTER, DROP, GRANT, DENY, REVOKE or UPDATE STATISTICS. Certain system stored procedures that perform DDL-like operations can also fire DDL triggers.
Use DDL triggers when you want to do the following:

  • Prevent certain changes to your database schema.
  • Have something occur in the database in response to a change in your database schema.
  • Record changes or events in the database schema.

2.8 Check Constraints

Constraints are rules that the SQL Server Database Engine provides for you. CHECK constraints enforce domain integrity by limiting the values that are accepted by one or more columns. You can create a CHECK constraint with any logical (Boolean) expression that returns TRUE or FALSE based on the logical operators.

CHECK constraints are a good way to implement business logic in a database.

3 Drill Down Into the Data Model

The above scripts gave us an idea about the “top level” objects in our database. Sometimes we need to get more information on the table, including columns and their data types, default values, keys, and indexes.
The following queries provide the “reverse engineering” of an existing data model.

3.1 Columns

The following script gets the tables and columns from the entire database. You can copy the query result into an Excel file, and then set up filters and sorting, and get acquainted with the data types used in the database.

3.1.1 Column Defaults

The default value is the value that will be saved if no value is specified for a column while inserting. Often, for a column that stores dates is getdate() or current_timestamp. Another common default in auditing is system_user, to identify the login that performed a certain action.

3.1.2 Computed Columns

A computed column is a virtual column that is not physically stored in the table unless the column is marked PERSISTED. A computed column expression can use data from other columns to calculate a value for the column to which it belongs. You can specify an expression for a computed column in in SQL Server 2014 by using SQL Server Management Studio or Transact-SQL.

Note that a computed column cannot be used as a DEFAULT or FOREIGN KEY constraint definition or with the NOT NULL constraint definition. However, if the computed column value is defined by a deterministic expression and the data type of the result is allowed in index columns, a computed column can be used as a key column in an index or as part of any PRIMARY KEY or UNIQUE constraint. For example, if the table has integer columns a and b, the computed column a + b may be indexed, but computed column a + DATEPART(dd, GETDATE()) cannot be indexed because the value might change in subsequent invocations. Also, a computed column cannot be the target of an INSERT or UPDATE statement.

3.1.3 Identity Columns

Identity columns can be used for generating key values. The identity property on a column guarantees the following:

  • Each new value is generated based on the current seed & increment.
  • Each new value for a particular transaction is different from other concurrent transactions on the table.

3.2 Keys and Indexes

To see which indexes exist on all tables in the current database.

To see which indexes are missing?

3.2.1 Foreign Keys

A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables to control the data that can be stored in the foreign key table. In a foreign key reference, a link is created between two tables when the column or columns that hold the primary key value for one table are referenced by the column or columns in another table. This column becomes a foreign key in the second table.

The result will be as follows:
Foreign_Keys_Output

3.2.3 Missing Indexes that support Foreign Keys

It is recommended to have an index associated with each foreign key. This ensures faster table joins, which are typically joined on foreign key columns anyway. Indexes on foreign keys also provide faster deletes. If these supporting indexes are missing, SQL will perform a table scale on the related table each time a record in the first table is deleted.

3.3 Object Dependencies

Some database objects have dependencies upon other database objects. For example, views and stored procedures depend on the existence of tables that contain the data returned by the view or procedure. An object that references another object in its definition and that definition is stored in the system catalog is called a referencing entity. An object that is referred to by another object is called a referenced entity.
sp_msdependencies is an SQL Server undocumented stored procedure that can be helpful in navigating complex table interdependencies.

If we list all dependencies using sp_msdependencies, it will return four columns: Type, ObjName, Owner (Schema) and Sequence.

3.3.1 Query the System Catalog Views

To query the foreign key relationships system tables:

3.3.2 Using a Common Table Expression (CTE)

To solve a recursive query using a Common Table Expression (CTE).

Summary

You can spend a couple of hours to get acquainted with any database design by using best practices provided in this article.
This article provides sample scripts that you can copy and run on the SQL Server and a database you are currently using.

Andrey Langovoy

Andrey Langovoy

Andrey Langovoy is a team leader at Devart. He takes part in development and testing database management tools for SQL Server, writes articles about SQL Server and contributes to open source projects, MSDN and MDN.
Andrey Langovoy

Latest posts by Andrey Langovoy (see all)

Andrey Langovoy

Andrey Langovoy is a team leader at Devart. He takes part in development and testing database management tools for SQL Server, writes articles about SQL Server and contributes to open source projects, MSDN and MDN.

  • Javeed Ahmed

    Good article.
    Everything at one place.

    Thanks
    Javeed