Daniel Calbimonte

The SQL Server system views/tables/functions. Common questions and solutions to real life problems

December 25, 2014 by

Introduction

In this new article, we will talk about the system views/tables/functions and how to solve common questions using them.

The system views are views that contain internal information about a Database.

The master database for example contains information about the SQL Server itself, while the msdb database contain information about the SQL Server agent and each database has its own system views/tables.

In this article we will show how to get the list of tables, views, stored procedures, how to get a list of tables of all the databases, how to find a table in multiple datatabases, how to get the list of users, logins, mapped logins, how to detect a fragmentation in a table and more.

Let’s start with some common questions about databases that can be solved with the system views/tables/functions:

How can I get the list of tables in a database?

The following queries can provide you with that information about the database tables:

Option 1

Option 2

You can use schema views (the INFORMATION_CHEMA.TABLES) or the sysobjects views directly. According to the books, it is better to use the INFORMATION_SCHEMA views because the internal structure will not change in the future. The sysobjects view contains useful information about the different database objects.

Xtype is the type of object and the possible values are:

SQ = Service Queue
TA = Assembly (CLR) DML trigger
TF = Table-valued-Function
TR = Trigger
TT = Table Type
U = User Table
UQ = Unique Constraint
V = View
X = Extended stored procedure

For more information about the sysobjects view you can go to the references.

How can I get the list of views in a database?

The solution is similar than the list of tables. There are 2 options to solve this problem:

Option 1

Option 2

[INFORMATION_SCHEMA].[VIEWS] contains information about the views or you can find the information in the sysobjects view.

How can I get the list of procedures in a database?

There are 2 options to solve this problem:

[INFORMATION_SCHEMA].[ROUTINES] contains information about stored procedures and functions. The sysobjects is a second option to get this information.

How can I get the creation date of a specific table?

The following T-SQL code shows the creation date of the table ProductDocument:

Crdate is the creation date and sysobjects is the view that contains most of the database objects including tables, views, stored procedures and functions.

How can I get the list of all the tables in all the databases?

The sp_MSforeachdb, is a very useful stored procedure that helps to work with all the databases. For some reason, this stored procedure is not documented.

The ? is the database name. It will show the result of the query in each database.

How can I search a table in all the databases?

This is a classical problem. The user created a table, but he does not remember where it was created and there are multiple databases where we need to search.

The following example shows how to find a table named test in all the SQL Server databases. The query will show the database(s) and the table.

How can I get the list of stored procedures names and their code?

The syscomments view contains very useful information about the stored procedure, triggers, checks and other SQL Server objects. You can check the code of your procedures, triggers and other objects. The sysobjects contains the stored procedure name and the syscomments the code. This query is very useful to find some words in multiple stored procedures at the same time.

How can I get the creation date of a specific database user?

The sysusers view contain useful information related to the users in a database. The following sample shows the creation date of the public database user.

For more information about the sysusers, go to references.

How can I get the creation date of a specific SQL Server Login?

The Login allows to login to the SQL Server database and the database user contains permissions to access to specific databases. The following query shows the creation Date of a specific Login:

How can I get the Login and the user mapped in all the databases?

This information is in the sys.database.principals and syss,server.principals. You will find the Login and the database user mapped to it.

How can I detect the fragmentation of a specific table?

Multiple inserts, deletes can produce fragmentations in the databases. In earlier versions, the DBCC SHOWCONTIG sentences were used to detect fragmentations. Now, we use the sys.dm_db_index_physical_stats function. The column that you need to check is the avg_fragmentation_in_percent. If the percentage is high, you may need to reorganize or rebuild the indexes of your table.

The following sample with shows the fragmentation information about the [Purchasing].[PurchaseOrderDetail table.

If the fragmentation percentage is higher than 30%, it is better to rebuild the index. If it is lower than 30%, you can just reorganize the index.

How can I get the information about the SQL Server sessions?

The [sys].[dm_exec_sessions] view contains useful information about the sessions like the start time, end time, login name, NT domain, program used and more.

How can I get information about the creation date of a backup?

You can find the start date and end date of a database backup in the backupset table.

How can I get information about the size in MB and location of a backup?

You can also find very useful information about the backup file size and the path of the backup using the backupfile system table. The following sample shows the size of the backups in MB and the path where it is located. This information is stored in the msdn database. The backup file_size is stored in bytes, that is why it is necessary to convert the value to MB.

How can I get information about creation date and modification date of a SQL Server Job?

There is a lot of information about SQL jobs in the MSDB database, dbo.sysjobs table. All the information related to the agent is stored in the MSDB database.

This information can also be displayed using the help_job stored procedure:

Conclusion

The system/table/functions helps us to monitor our database, track changes and measure the performance of the databases. There are multiple uses of these objects to help us in our real life problems.

Daniel Calbimonte
Latest posts by Daniel Calbimonte (see all)
168 Views