{"id":2154,"date":"2016-01-27T00:00:00","date_gmt":"2016-01-27T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/sql-server-system-views-the-basics\/"},"modified":"2021-08-16T15:01:55","modified_gmt":"2021-08-16T15:01:55","slug":"sql-server-system-views-the-basics","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/learn\/sql-server-system-views-the-basics\/","title":{"rendered":"SQL Server System Views: The Basics"},"content":{"rendered":"<div id=\"pretty\">\n<p>SQL Server  provides an assortment of system views for accessing metadata about the server  environment and its database objects. There are catalog views and information  schema views and dynamic management views and several other types of views. DBAs  and developers alike can benefit significantly from the rich assortment of  information they can derive through these views, and it is worth the effort to  get to know them. <\/p>\n<p>System views  are divided into categories that each serve a specific purpose. The most  extensive category is the one that contains catalog views. Catalog views let you  retrieve information about a wide range of system and database components-from  table columns and data types to server-wide configurations.<\/p>\n<p>Information  schema views are similar to some of the catalog views in that they provide  access to metadata that describes database objects such as tables, columns,  domains, and check constraints. However, information schema views conform to the  ANSI standard, whereas catalog views are specific to SQL Server.<\/p>\n<p>In contrast  to either of these types of views, dynamic management views return server state  data that can be used to monitor and fine-tune a SQL Server instance and its  databases. Like catalog views, dynamic management views are specific to SQL  Server.<\/p>\n<p>In this  article, we&#8217;ll focus on these three types of views, looking at examples in each  category. We won&#8217;t be covering the other types of system views because they tend  not to be as commonly used, with perhaps a couple exceptions. For the most part,  catalog, information schema, and dynamic management views are the ones you&#8217;ll  likely be using the most often. But just so you know, the other types are  related to replication and data-tier application (DAC) instances as well as  provide compatibility with earlier SQL Server releases. Although they have their  places, for now we&#8217;ll stick with the big three.<\/p>\n<h1>Catalog views<\/h1>\n<p>Of the  various types of system views available in SQL Server, catalog views represent  the largest collection and most diverse. You can use catalog views to gather  information about such components as AlwaysOn Availability Groups, Change Data  Capture, change tracking, database mirroring, full-text search, Resource  Governor, security, Service Broker, and an assortment of other features-all in  addition to being able to view information about the database objects  themselves.<\/p>\n<p>In fact, SQL  Server provides so many catalog views that it would be nearly impossible-or at  least highly impractical-to try look at all of them in one article, but know  that there is a vast storehouse of views waiting for you, and they all work  pretty much the same way. <\/p>\n<p>Microsoft  recommends that you use catalog views as your primary method for accessing SQL  Server metadata because they provide the most efficient mechanism for retrieving  this type of information. Through the catalog views you can access all  user-available metadata. For example, the following <b><code>SELECT<\/code><\/b>  statement returns information about databases whose name starts with <i>adventureworks<\/i>:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT name, database_id, compatibility_level\nFROM sys.databases\nWHERE name LIKE 'adventureworks%';\n<\/pre>\n<p>The columns  specified in the <b> <code>SELECT<\/code><\/b> clause-<b><code>name<\/code><\/b>, <b> <code>database_id<\/code><\/b>, and <b> <code>compatibility_level<\/code><\/b>-represent  only a fraction of the many columns supported by this view. The view will  actually return nearly 75 columns worth of information about each database  installed on the SQL Server instance. I&#8217;ve kept it short for the sake of  brevity, as shown in the following results: <\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tname<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tdatabase_id<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tcompatibility_level<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>AdventureWorks2014<\/p>\n<\/td>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<td valign=\"top\">\n<p>120<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>AdventureWorksDW2014<\/p>\n<\/td>\n<td valign=\"top\">\n<p>10<\/p>\n<\/td>\n<td valign=\"top\">\n<p>120<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There is  nothing remarkable here, except for the ease with which I was able to collect  the metadata. The results include the database names, their auto-generated IDs,  and their compatibility levels, which in both cases is <b><code>120<\/code><\/b>. The <b> <code>120<\/code><\/b> refers to SQL Server 2014. (I created the examples in  this article on a local instance of SQL Server 2014 running in a test virtual  machine.)<\/p>\n<p>The <b> <code>sys.databases<\/code><\/b> view can also return information about  database settings, such as whether the database is read-only or whether the  auto-shrink feature is enabled. Many of the configuration-related columns take  the <b> bit<\/b> data type to indicate whether a feature is  on (<b>1<\/b>) or off (<b><code>0<\/code><\/b>).  <\/p>\n<p>As the  preceding example illustrates, you access catalog views through the <b> <code>sys<\/code><\/b> schema. Whichever view you use, it&#8217;s always a good idea  to check the SQL Server documentation if you have any questions about its  application to your particular circumstances. For example, the <b> <code>sys.databases<\/code><\/b> view includes the <b><code>state<\/code><\/b>  column, which provides status information such as whether a database is online,  offline, or being restored. Each option is represented by one of nine predefined <b> tinyint<\/b> values. Some values in this column  pertain only to certain environments. For instance, the value <b><code>7<\/code><\/b>  (copying) applies only to Azure SQL Database.<\/p>\n<p>Now let&#8217;s  look at the <b> sys.objects<\/b> catalog view, which returns a row  for each user-defined, schema-scoped object in a database. The following <b> <code>SELECT<\/code><\/b> statement retrieves the name and ID of all  table-valued functions defined in the <b> <code>dbo<\/code><\/b> schema within  the <b> <code>AdventureWorks2014<\/code><\/b> sample database:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT name, object_id\nFROM sys.objects\nWHERE SCHEMA_NAME(schema_id) = 'dbo'\n&#160; AND type_desc = 'sql_table_valued_function';\n&#160;\n<\/pre>\n<p>Notice that  I use the <b> SCHEMA_NAME<\/b> built-in function to match the  schema <b> ID<\/b> to <b> <code>dbo<\/code><\/b> in the <b> <code>WHERE<\/code><\/b> clause. Functions such as <b><code>SCHEMA_NAME<\/code><\/b>, <b> OBJECT_ID<\/b>, <b> <code>OBJECT_NAME<\/code><\/b>, and  so on can be extremely useful when working with catalog views. <\/p>\n<p>Also in the <b> <code>WHERE<\/code><\/b> clause, I match the <b><code>type_desc<\/code><\/b>  column to <b> sql_table_valued_function<\/b>, giving me the  following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tname<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tobject_id<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p> \t\tufnGetContactInformation<\/p>\n<\/td>\n<td valign=\"top\">\n<p>103671417<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <b> <code>sys.objects<\/code><\/b> view is a handy tool to have because it provides  quick and easy access to all user-defined objects in your database, including  tables, views, triggers, functions, and constraints. However, SQL Server also  provides catalog views that are distinct to a specific object type. For example,  the following <b> <code>SELECT<\/code><\/b> statement  retrieves data through the <b> <code>sys.tables<\/code><\/b> view:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT name, max_column_id_used\nFROM sys.tables\nWHERE SCHEMA_NAME(schema_id) = 'HumanResources'\n<\/pre>\n<p>The  statement returns a list of all tables in the <b> <code>HumanResources<\/code><\/b>  schema, along with the maximum column ID used for each table, as shown in the  following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tname<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tmax_column_id_used<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Shift<\/p>\n<\/td>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Department<\/p>\n<\/td>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Employee<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p> \t\tEmployeeDepartmentHistory<\/p>\n<\/td>\n<td valign=\"top\">\n<p>6<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>EmployeePayHistory<\/p>\n<\/td>\n<td valign=\"top\">\n<p>5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>JobCandidate<\/p>\n<\/td>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The  interesting thing about the <b> <code>sys.tables<\/code><\/b> view is  that it inherits all the columns from the <b> <code>sys.objects<\/code><\/b> view  and then adds additional columns with table-specific information. For example,  in the preceding example, the <b> <code>name<\/code><\/b> column is  inherited from <b> <code>sys.objects<\/code><\/b> but the <b>max_column_id_used<\/b> column is specific to <b><code>sys.tables<\/code><\/b>.<b> <\/b>(For information about which views inherit columns from other views, refer  to the SQL Server documentation.)<b><\/b><\/p>\n<p>You can also  join catalog views to retrieve specific types of information. For example, the  following <b> SELECT<\/b> statement joins the <b><code>sys.columns<\/code><\/b>  view to the <b> sys.types<\/b> view to retrieve information about the <b> Person<\/b> table:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT c.name AS ColumnName,&#160; \n&#160; t.name AS DataType, \n&#160; CASE t.is_user_defined\n&#160;&#160;&#160; WHEN 1 THEN 'user-defined type'\n&#160;&#160;&#160; ELSE 'system type' END AS UserOrSystem\nFROM sys.columns c JOIN sys.types t\n&#160; ON c.user_type_id = t.user_type_id\nWHERE c.object_id = OBJECT_ID('Person.Person');\n<\/pre>\n<p>Not  surprisingly, the <b> <code>sys.columns<\/code><\/b> view  returns a list of columns in the table, and the <b><code>sys.types<\/code><\/b>  view returns the name of the column data types, along with whether they are  system types or user-defined:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tColumnName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tDataType<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tUserOrSystem<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>BusinessEntityID<\/p>\n<\/td>\n<td valign=\"top\">\n<p>int<\/p>\n<\/td>\n<td valign=\"top\">\n<p>system type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>PersonType<\/p>\n<\/td>\n<td valign=\"top\">\n<p>nchar<\/p>\n<\/td>\n<td valign=\"top\">\n<p>system type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NameStyle<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NameStyle<\/p>\n<\/td>\n<td valign=\"top\">\n<p>user-defined type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Title<\/p>\n<\/td>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<td valign=\"top\">\n<p>system type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>FirstName<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Name<\/p>\n<\/td>\n<td valign=\"top\">\n<p>user-defined type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>MiddleName<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Name<\/p>\n<\/td>\n<td valign=\"top\">\n<p>user-defined type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>LastName<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Name<\/p>\n<\/td>\n<td valign=\"top\">\n<p>user-defined type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Suffix<\/p>\n<\/td>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<td valign=\"top\">\n<p>system type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>EmailPromotion<\/p>\n<\/td>\n<td valign=\"top\">\n<p>int<\/p>\n<\/td>\n<td valign=\"top\">\n<p>system type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>AdditionalContactInfo<\/p>\n<\/td>\n<td valign=\"top\">\n<p>xml<\/p>\n<\/td>\n<td valign=\"top\">\n<p>system type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Demographics<\/p>\n<\/td>\n<td valign=\"top\">\n<p>xml<\/p>\n<\/td>\n<td valign=\"top\">\n<p>system type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>rowguid<\/p>\n<\/td>\n<td valign=\"top\">\n<p>uniqueidentifier<\/p>\n<\/td>\n<td valign=\"top\">\n<p>system type<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ModifiedDate<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<td valign=\"top\">\n<p>system type<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Up to this  point, the catalog views we&#8217;ve looked at have focused on the databases and their  objects. However, we can use catalog views to retrieve all sorts of information,  such as details about database files:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT file_id, name, state_desc, type_desc\nFROM sys.database_files\nWHERE name LIKE 'adventureworks%';\n<\/pre>\n<p>In this  case, we&#8217;re using the <b> <code>sys.database_files<\/code><\/b> view to retrieve the  file ID, file name, file state, and file type.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tfile_id<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tname<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tstate_desc<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\ttype_desc<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p> \t\tAdventureWorks2014_Data<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ONLINE<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ROWS<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p> \t\tAdventureWorks2014_Log<\/p>\n<\/td>\n<td valign=\"top\">\n<p>ONLINE<\/p>\n<\/td>\n<td valign=\"top\">\n<p>LOG<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We might  instead use the <b> sys.assembly_types<\/b> view to return information  about any assemblies added to the database:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT name, user_type_id, assembly_class\nFROM sys.assembly_types;\n<\/pre>\n<p>As the  following results show, the <b> <code>AdventureWorks2014<\/code><\/b>  database includes three assemblies, all of which are SQL Server&#8217;s advanced data  types:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tname<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tuser_type_id<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tassembly_class<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>hierarchyid<\/p>\n<\/td>\n<td valign=\"top\">\n<p>128<\/p>\n<\/td>\n<td valign=\"top\">\n<p> \t\tMicrosoft.SqlServer.Types.SqlHierarchyId<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>geometry<\/p>\n<\/td>\n<td valign=\"top\">\n<p>129<\/p>\n<\/td>\n<td valign=\"top\">\n<p> \t\tMicrosoft.SqlServer.Types.SqlGeometry<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>geography<\/p>\n<\/td>\n<td valign=\"top\">\n<p>130<\/p>\n<\/td>\n<td valign=\"top\">\n<p> \t\tMicrosoft.SqlServer.Types.SqlGeography<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>You can even  retrieve data about security-related metadata within your database. For example,  the following <b> <code>SELECT<\/code><\/b> statement  uses the <b> sys.database_principals<\/b> view to return the names  and IDs of all security principals in the <b><code>AdcentureWorks2014<\/code><\/b>  database:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT name, principal_id\nFROM sys.database_principals\nWHERE type_desc = 'DATABASE_ROLE';\n<\/pre>\n<p>Notice that  we&#8217;ve used a <b> <code>WHERE<\/code><\/b> clause to qualify our query so the <b> SELECT<\/b> statement returns only the <b> <code>DATABASE_ROLE<\/code><\/b> principal type:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tname<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tprincipal_id<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>public<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>db_owner<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16384<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>db_accessadmin<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16385<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>db_securityadmin<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16386<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>db_ddladmin<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16387<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>db_backupoperator<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16389<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>db_datareader<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16390<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>db_datawriter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16391<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>db_denydatareader<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16392<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>db_denydatawriter<\/p>\n<\/td>\n<td valign=\"top\">\n<p>16393<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Of course,  SQL Server security occurs at the database level and at the server level. To  address the server level, SQL Server also includes catalog views specific to the  current instance. For example, the following <b><code>SELECT<\/code><\/b>  statement joins the <b> <code>sys.server_principals<\/code><\/b>  view and the <b> <code>sys.server_permissions<\/code><\/b> view to retrieve  information about the server principals and their permissions:<b><\/b><\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT pr.name, pr. principal_id, \n&#160; pm.permission_name, pm.state_desc\nFROM sys.server_principals pr \n&#160; JOIN sys.server_permissions AS pm\n&#160; ON pr.principal_id = pm.grantee_principal_id \nWHERE pr.type_desc = 'SERVER_ROLE';\n<\/pre>\n<p>In this  case, we&#8217;re concerned only with the <b> <code>SERVER_ROLE<\/code><\/b>  principal type, so we&#8217;ve added the <b> <code>WHERE<\/code><\/b> clause,  giving us the following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tname<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tprincipal_id<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\toermission_name<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tstate_desc<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>public<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>VIEW ANY DATABASE<\/p>\n<\/td>\n<td valign=\"top\">\n<p>GRANT<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>public<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>CONNECT<\/p>\n<\/td>\n<td valign=\"top\">\n<p>GRANT<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>public<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>CONNECT<\/p>\n<\/td>\n<td valign=\"top\">\n<p>GRANT<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>public<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>CONNECT<\/p>\n<\/td>\n<td valign=\"top\">\n<p>GRANT<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>public<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<td valign=\"top\">\n<p>CONNECT<\/p>\n<\/td>\n<td valign=\"top\">\n<p>GRANT<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>You can also  use catalog views to retrieve server configuration information. For instance,  the following <b> <code>SELECT<\/code><\/b> statement uses the the <b> <code>sys.configurations<\/code><\/b> view to retrieve configuration  information about the current server:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT name, description\nFROM sys.configurations\nWHERE is_advanced = 1 AND is_dynamic = 0;\n<\/pre>\n<p>In this  case, we&#8217;ve limited our query to non-dynamic advanced settings, as shown in the  following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tname<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tdescription<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>user connections<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Number of user  \t\tconnections allowed<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>locks<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Number of locks for  \t\tall users<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>open objects<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Number of open  \t\tdatabase objects<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>fill factor (%)<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Default fill factor  \t\tpercentage<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>c2 audit mode<\/p>\n<\/td>\n<td valign=\"top\">\n<p>c2 audit mode<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>priority boost<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Priority boost<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>set working set size<\/p>\n<\/td>\n<td valign=\"top\">\n<p>set working set size<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>lightweight pooling<\/p>\n<\/td>\n<td valign=\"top\">\n<p>User mode scheduler  \t\tuses lightweight pooling<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>scan for startup  \t\tprocs<\/p>\n<\/td>\n<td valign=\"top\">\n<p>scan for startup  \t\tstored procedures<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>affinity I\/O mask<\/p>\n<\/td>\n<td valign=\"top\">\n<p>affinity I\/O mask<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>affinity64 I\/O mask<\/p>\n<\/td>\n<td valign=\"top\">\n<p>affinity64 I\/O mask<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>common criteria  \t\tcompliance enabled<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Common Criteria  \t\tcompliance mode enabled<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There are,  of course, many more examples of catalog views I can show you, but you get the  point. There&#8217;s a great deal of information to be had, and I&#8217;ve barely scratched  the surface. For a complete listing of the available catalog views, check out  the MSDN topic <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms174365.aspx\"> Catalog Views (Transact-SQL)<\/a>.<\/p>\n<h1>Information schema views<\/h1>\n<p>Information  schema views provide a standardized method for querying metadata about objects  within a database. The views are part of the schema <b><code>INFORMATION_SCHEMA<\/code><\/b>,  rather than the <b> <code>sys<\/code><\/b> schema, and are much more limited in  scope than catalog views. At last count, SQL Server was providing only 21  information schema views, compared to over 200 catalog views.<\/p>\n<p>The  advantage of using information schema views is that, because they are  ANSI-compliant, you can theoretically migrate your code to different database  systems without having to update your view references. If portability is  important to your solution, you should consider information schema views, just  know that they don&#8217;t do nearly as much as catalog views. And, of course, using  one type of view doesn&#8217;t preclude you from using another type of view.<\/p>\n<p>With  information schema views, you can retrieve metadata about database objects such  as tables, constraints, columns, privileges, views, and domains. (In the world  of information schema views, a <i>domain<\/i>  is a user-defined data type, and a <i>catalog<\/i>  is the database itself.)<\/p>\n<p>Let&#8217;s look  at a few examples. The first one uses the <b> <code>TABLES<\/code><\/b> view to  retrieve the name and type of all the tables and views in the <b><code>Purchasing<\/code><\/b>  schema:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT TABLE_NAME, TABLE_TYPE\nFROM INFORMATION_SCHEMA.TABLES\nWHERE TABLE_SCHEMA = 'purchasing'\nORDER BY TABLE_NAME;\n<\/pre>\n<p>No magic  here. Just a simple query that returns basic information, as shown in the  following results:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tTABLE_NAME<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tTABLE_TYPE<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ProductVendor<\/p>\n<\/td>\n<td valign=\"top\">\n<p>BASE TABLE<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>PurchaseOrderDetail<\/p>\n<\/td>\n<td valign=\"top\">\n<p>BASE TABLE<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>PurchaseOrderHeader<\/p>\n<\/td>\n<td valign=\"top\">\n<p>BASE TABLE<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ShipMethod<\/p>\n<\/td>\n<td valign=\"top\">\n<p>BASE TABLE<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Vendor<\/p>\n<\/td>\n<td valign=\"top\">\n<p>BASE TABLE<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>vVendorWithAddresses<\/p>\n<\/td>\n<td valign=\"top\">\n<p>VIEW<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>vVendorWithContacts<\/p>\n<\/td>\n<td valign=\"top\">\n<p>VIEW<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We could  have also retrieved the <b> <code>TABLE_CATALOG<\/code><\/b> and <b> <code>TABLE_SCHEMA<\/code><\/b> columns, which are included in the view to  provide fully qualified, four-part names for each object, but we didn&#8217;t need  that information in this case, and the table includes no other columns, falling  far short of what you get with <b> <code>sys.tables<\/code><\/b>.<\/p>\n<p>Now let&#8217;s  pull data through the <b> <code>COLUMNS<\/code><\/b> view, which provides a few more  details than we get with <b> <code>TABLES<\/code><\/b>:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT COLUMN_NAME, DATA_TYPE, DOMAIN_NAME\nFROM INFORMATION_SCHEMA.COLUMNS\nWHERE TABLE_SCHEMA = 'person' \n&#160; AND TABLE_NAME = 'contacttype';\n<\/pre>\n<p>In this  case, our query retrieves the column name, system data type, and user data type,  if any, within the <b> <code>Person.ContactType<\/code><\/b> table. In this case,  the table includes only one user-defined data type (<b><code>Name<\/code><\/b>):<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tCOLUMN_NAME<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tDATA_TYPE<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tDOMAIN_NAME<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ContactTypeID<\/p>\n<\/td>\n<td valign=\"top\">\n<p>int<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Name<\/p>\n<\/td>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<td valign=\"top\">\n<p>Name<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ModifiedDate<\/p>\n<\/td>\n<td valign=\"top\">\n<p>datetime<\/p>\n<\/td>\n<td valign=\"top\">\n<p>NULL<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now suppose  we want to retrieve a list of user-defined data types in the <b> <code>AdventureWorks2014<\/code><\/b> database, along with the base type for  each one:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT DOMAIN_NAME, DATA_TYPE\nFROM INFORMATION_SCHEMA.DOMAINS\nORDER BY DOMAIN_NAME;\n<\/pre>\n<p>This time,  we use the <b> DOMAINS<\/b> view, which gives us the following  results (at least on my system):<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tDOMAIN_NAME<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tDATA_TYPE<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>AccountNumber<\/p>\n<\/td>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Flag<\/p>\n<\/td>\n<td valign=\"top\">\n<p>bit<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Name<\/p>\n<\/td>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>NameStyle<\/p>\n<\/td>\n<td valign=\"top\">\n<p>bit<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>OrderNumber<\/p>\n<\/td>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Phone<\/p>\n<\/td>\n<td valign=\"top\">\n<p>nvarchar<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let&#8217;s look  at one more example, this one of the <b> <code>CHECK_CONSTRAINTS<\/code><\/b>  view, which retrieves information about the check constraints in the <b> <code>Person<\/code><\/b> schema:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT CONSTRAINT_NAME, CHECK_CLAUSE\nFROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS\nWHERE CONSTRAINT_SCHEMA = 'person';\n<\/pre>\n<p>In this  case, we get the name of the check constraints, along with the constraint  definitions:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tCONSTRAINT_NAME<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tCHECK_CLAUSE<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p> \t\tCK_Person_EmailPromotion<\/p>\n<\/td>\n<td valign=\"top\">\n<p> \t\t([EmailPromotion]&gt;=(0) AND [EmailPromotion]&lt;=(2))<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>CK_Person_PersonType<\/p>\n<\/td>\n<td valign=\"top\">\n<p>([PersonType] IS NULL  \t\tOR (upper([PersonType])=&#8217;GC&#8217; OR upper([PersonType])=&#8217;SP&#8217; OR  \t\tupper([PersonType])=&#8217;EM&#8217; OR upper([PersonType])=&#8217;IN&#8217; OR  \t\tupper([PersonType])=&#8217;VC&#8217; OR upper([PersonType])=&#8217;SC&#8217;))<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>That&#8217;s all  there is to information schema views. There are relatively few of them and the  ones that are there contain relatively little information, when compared to  their catalog counterparts. You can find more details about information schema  views by referring to the MSDN topic <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms186778.aspx\">Information  Schema Views (Transact-SQL)<\/a>.<\/p>\n<h1>Dynamic management views<\/h1>\n<p>With dynamic  management views, we move into new territory. The views return server state  information about your databases and servers, which can be useful for monitoring  your systems, tuning performance, and diagnosing any issues that might arise.<\/p>\n<p>Like catalog  views, dynamic management views provide a wide range of information. For  example, SQL Server includes a set of dynamic management that are specific to  memory-optimized tables. One of these, <b> <code> dm_xtp_system_memory_consumers<\/code><\/b>, returns information about  database-level memory consumers:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT memory_consumer_desc, allocated_bytes, used_bytes\nFROM sys.dm_xtp_system_memory_consumers\nWHERE memory_consumer_type_desc = 'pgpool';\n<\/pre>\n<p>The  statement retrieves the consumer description, the amount of allocated bytes, and  the amount of used bytes for the <b> <code>pgpool<\/code><\/b> consumer  type, giving us the following results.<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tmemory_consumer_desc<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tallocated_bytes<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tused_bytes<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>System 256K page pool<\/p>\n<\/td>\n<td valign=\"top\">\n<p>262144<\/p>\n<\/td>\n<td valign=\"top\">\n<p>262144<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>System 64K page pool<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>System 4K page pool<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Like catalog  views, dynamic management views are part of the <b><code>sys<\/code><\/b>  schema. In addition, their names always begin with the <b><code>dm_<\/code><\/b>  prefix. Unfortunately, Microsoft uses the same naming convention for SQL  Server&#8217;s dynamic management functions. But you&#8217;ll quickly discover which ones  are which when you try to run them and you&#8217;re prompted to provide input  parameters. (I&#8217;ll save a discussion about the functions for a different  article.)<\/p>\n<p>Another  category of dynamic management views focuses on the SQL Server Operating System  (SQLOS), which manages the operating system resources specific to SQL Server.  For example, you can use the <b> <code>dm_os_threads<\/code><\/b> view  to retrieve a list of SQLOS threads running under the current SQL Server  process:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT os_thread_id, kernel_time, usermode_time\nFROM sys.dm_os_threads\nWHERE usermode_time &gt; 300;\n<\/pre>\n<p>The  statement returns the thread ID, kernel time, and user time, for those threads  greater than 300 milliseconds, giving us the following results (on my test  system):<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tos_thread_id<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tkernel_time<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tusermode_time<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2872<\/p>\n<\/td>\n<td valign=\"top\">\n<p>140<\/p>\n<\/td>\n<td valign=\"top\">\n<p>327<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2928<\/p>\n<\/td>\n<td valign=\"top\">\n<p>15<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1014<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>2944<\/p>\n<\/td>\n<td valign=\"top\">\n<p>46<\/p>\n<\/td>\n<td valign=\"top\">\n<p>327<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>5500<\/p>\n<\/td>\n<td valign=\"top\">\n<p>78<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1216<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The SQLOS  views even include one that returns miscellaneous information about the computer  and its resources:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT cpu_count, physical_memory_kb, virtual_memory_kb\nFROM sys.dm_os_sys_info;\n<\/pre>\n<p>Although the <b> <code>dm_os_sys_info<\/code><\/b> view can return a variety of information  about the environment, in this case, we&#8217;ve limited that information to the CPU  count, physical memory, and virtual memory:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tcpu_count<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tphysical_memory_kb<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tvirtual_memory_kb<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>4193840<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8589934464<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>SQL Server  also includes dynamic management views for retrieving information about the  indexes. For example, you can use the <b> <code>db_index_usage_stats<\/code><\/b>  view to return details about different types of index operations:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">SELECT index_id, user_seeks, user_scans\nFROM sys.dm_db_index_usage_stats\nWHERE object_id = OBJECT_ID('AdventureWorks2014.HumanResources.Employee');\n<\/pre>\n<p>The  statement returns the data shown in the following table:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tindex_id<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tuser_seeks<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tuser_scans<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<td valign=\"top\">\n<p>9<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Being able  to query statistics about an index in this way can be useful when testing an  application&#8217;s individual operations. This can help you pinpoint whether your  queries are using the indexes effectively or whether you might need to build  different indexes. Note, however, that index statistics can reflect all  activity, whether generated by an application or generated internally by SQL  Server.<\/p>\n<p>Dynamic  management views are either server-scoped or database-scoped. The ones we&#8217;ve  look at so far have been server-scoped, even the <b><code> dm_db_index_usage_stats<\/code><\/b> index shown in the last example. In that  case, however, we were concerned with only the <b> AdventureWorks2014<\/b> database, so we specified the  database in our <b> <code>WHERE<\/code><\/b> clause.<\/p>\n<p>If you want  to run a database-scoped dynamic management view, you must do so within the  context of the target database. In the following <b><code>SELECT<\/code><\/b>  statement, I use the <b> <code>dm_db_file_space_usage<\/code><\/b>  view to return space usage data about the data file used by the <b> <code>AdventureWorks2014<\/code><\/b> database: <\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT total_page_count, allocated_extent_page_count, unallocated_extent_page_count\nFROM sys.dm_db_file_space_usage\nWHERE file_id = 1;\n<\/pre>\n<p>All I&#8217;m  doing here is retrieving the total page count, allocated extent page count, and  unallocated extent page count:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\ttotal_page_count<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tallocated_extent_page_count<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tunallocated_extent_page_count<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>30368<\/p>\n<\/td>\n<td valign=\"top\">\n<p>28368<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2000<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Note that  page counts are always at the extent level, which means they will be multiples  of eight.<\/p>\n<p>We can  instead use the <b> <code>dm_db_fts_index_physical_stats<\/code><\/b> view to  retrieve data about the full-text and sematic indexes in each table:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT OBJECT_NAME(object_id) ObjectName, \n&#160; object_id ObjectID, fulltext_index_page_count IndexPages\nFROM sys.dm_db_fts_index_physical_stats;\n<\/pre>\n<p>This time we  get the object name and ID of the table that contains the index, as well as the  page count for each index:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tObjectName<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tObjectID<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tIndexPages<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>ProductReview<\/p>\n<\/td>\n<td valign=\"top\">\n<p>610101214<\/p>\n<\/td>\n<td valign=\"top\">\n<p>8<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Document<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1077578877<\/p>\n<\/td>\n<td valign=\"top\">\n<p>13<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>JobCandidate<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1589580701<\/p>\n<\/td>\n<td valign=\"top\">\n<p>15<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let&#8217;s look  at one more dynamic management view that is database-scoped. The <b> <code>dm_db_persisted_sku_features<\/code><\/b> view returns a list of  edition-specific features that are enabled in the current database, but are not  supported on all SQL Server versions. The view applies to SQL Server 2008  through the current version. The following <b> <code>SELECT<\/code><\/b> statement  uses the view to retrieve the feature name and ID:<\/p>\n<pre class=\"theme:ssms2012 lang:tsql\">USE AdventureWorks2014;\ngo\n&#160;\nSELECT feature_name, feature_id\nFROM sys.dm_db_persisted_sku_features;\n<\/pre>\n<p>In this  case, the <b> SELECT<\/b> statement returns only one row:<\/p>\n<table class=\"MsoTableGrid\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<p><b> \t\tfeature_name<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b> \t\tfeature_id<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>InMemoryOLTP<\/p>\n<\/td>\n<td valign=\"top\">\n<p>800<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#160;The <b> <code>dm_db_persisted_sku_features<\/code><\/b> view includes the <b> <code>feature_id<\/code><\/b> column only for informational purposes. The  column is not supported and may not be part of the view in the future.  <\/p>\n<p>Although  these are but a few of the dynamic management views that SQL Server supports,  the examples should give you a good sense of the variety of data they can  provide. For a complete list of dynamic management views and to learn more about  each one, refer to the MSDN topic <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms188754.aspx\">Dynamic  Management Views and Functions (Transact-SQL)<\/a>.<\/p>\n<h1>Plenty more where that came  from<\/h1>\n<p>As mentioned  earlier, SQL Server also provides system views to support backward  compatibility, replication, and DAC instances. The compatibility views might  come in handy if you&#8217;re still running SQL Server 2000. You might also find the  replication-related views useful if you&#8217;ve implemented replication, although  Microsoft recommends that you instead use the stored procedures available for  accessing replication metadata. As for the DAC views, SQL Server provides only  two of them, and they reside only in the <b> <code>msdb<\/code><\/b> database.<\/p>\n<p>For many  DBAs and database developers, the catalog views and dynamic management views  will likely be their first line of defense when retrieving SQL Server metadata,  whether it&#8217;s specific to particular database objects or the server environment  as a whole. That&#8217;s not to diminish the importance of the other views, but rather  to point out that Microsoft has put most of its effort into building an  extensive set of catalog views and dynamic management views. And given all the  work that&#8217;s gone into them, there&#8217;s certainly no reason not to take advantage of  what&#8217;s available.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>When maintaining or refactoring an unfamiliar database, you&#8217;ll need a fast way to uncover all sorts of facts about the database, its tables, columns keys and indexes. SQL Server&#8217;s plethora of system catalog views, INFORMATION_SCHEMA views, and dynamic management views contain all the metadata you need, but it isn&#8217;t always obvious which views are best to use for which sort of information. Many of us could do with a simple explanation, and who better to provide one than Rob Sheldon?&hellip;<\/p>\n","protected":false},"author":221841,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143525],"tags":[4242,4168,4149,4500,4150,4151],"coauthors":[],"class_list":["post-2154","post","type-post","status-publish","format-standard","hentry","category-learn","tag-basics","tag-database","tag-learn-sql-server","tag-refactoring","tag-sql","tag-sql-server"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221841"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2154"}],"version-history":[{"count":4,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2154\/revisions"}],"predecessor-version":[{"id":41270,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2154\/revisions\/41270"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2154"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}