ptg
174
CHAPTER 7 SQL Server System and Database Administration
TABLE 7.2 SQL Server 2008 Alternatives for SQL Server 2000 System Tables
SQL Server 2000
System Table
SQL Server 2008 System View View Type
sys.foreign_keys
Catalog view
syscurconfigs sys.configurations
Catalog view
sysdatabases sys.databases
Catalog view
sysdepends sys.sql_dependencies
Catalog view
sysdevices sys.backup_devices
Catalog view
sysfilegroups sys.filegroups
Catalog view
sysfiles sys.database_files
Catalog view
sysforeignkeys sys.foreign_keys
Catalog view
sysfulltextcatalogs sys.fulltext_catalogs
Catalog view
sysindexes sys.indexes
Catalog view
sys.partitions
Catalog view
sys.allocation_units
Catalog view
Download from www.wowebook.com
ptg
175
System Views
7
TABLE 7.2 SQL Server 2008 Alternatives for SQL Server 2000 System Tables
SQL Server 2000
System Table
SQL Server 2008 System View View Type
sys.dm_exec_sessions
DMV
sys.dm_exec_requests
DMV
sysprotects sys.database_permissions
Catalog view
sys.server_permissions
Catalog view
sysreferences sys.foreign_keys
Catalog view
sysremotelogins sys.remote_logins
Catalog view
sysservers sys.servers
Catalog view
systypes sys.types
Catalog view
sysusers sys.database_principals
Catalog view
Catalog Views
Using catalog views is the preferred method for returning information used by the
Microsoft SQL Server database engine. There is a catalog view to return information about
returned and include elements that are new to SQL Server 2008.
To demonstrate the use of a catalog view, let’s compare a simple SQL Server 2000 SELECT
statement that returns object information to a SELECT statement in SQL Server 2008 that
returns similar information. The following example shows a SELECT statement written in
SQL Server 2000 to return any stored procedure created after a given date:
select o.crdate, o.name
from sysobjects o
where type = ‘p’
and crdate > ‘1/1/08’
order by crdate, name
Now, compare this SELECT statement to one that uses a SQL Server 2008 catalog view. The
sys.objects catalog view is a new alternative to the SQL Server 2000 sysobjects system
table. The following SELECT uses the sys.objects catalog view to return the same type of
information as the preceding example:
select o.create_date, o.modify_date, name
from sys.objects o
where type = ‘p’
and (create_date > ‘1/1/08’
or o.modify_date >= ‘1/1/08’)
order by 1, 2, 3
As you can see, the modify_date column has been added to the SELECT statement. This
column did not exist with the sysobjects system table. The addition of this column
allows you to identify objects that were created as well as objects that were modified or
altered.
Let’s look at an example of using a catalog view to return the same kind of information
returned in SQL Server 2000 with a system procedure. The handy sp_helpfile system
procedure returns information about database files associated with a given database. This
SQL Server 2000 procedure is still available in SQL Server 2008. An alternative to this
procedure is the new sys.master_files catalog view. This view returns all the information
that sp_helpfile returns and more. The following example shows a SELECT statement
packs to an existing version.
The information schema views also have the advantage of being SQL-92 compatible.
Compliance with the SQL-92 standard means that SQL statements written against these
views work with other DBMSs that also adhere to the SQL-92 standard. The SQL-92 stan-
dard supports a three-part naming convention, which SQL Server has implemented as
database.schema.object.
In SQL Server 2008, all the information schema views are in the same schema, named
INFORMATION_SCHEMA. The following information schema views or objects are available:
. CHECK_CONSTRAINTS
. COLUMN_DOMAIN_USAGE
. COLUMN_PRIVILEGES
. COLUMNS
. CONSTRAINT_COLUMN_USAGE
Download from www.wowebook.com
ptg
178
CHAPTER 7 SQL Server System and Database Administration
. CONSTRAINT_TABLE_USAGE
. DOMAIN_CONSTRAINTS
. DOMAINS
. KEY_COLUMN_USAGE
. PARAMETERS
. REFERENTIAL_CONSTRAINTS
. ROUTINES
. ROUTINE_COLUMNS
. SCHEMATA
. TABLE_CONSTRAINTS
. TABLE_PRIVILEGES
. TABLES
. VIEW_COLUMN_USAGE
available in SQL Server 2000. The SQL Server 2000 diagnostic tools, such as heavy Profiler
traces, PerfMon, dbcc executions, and pssdiag, are still available, but oftentimes, the
information returned from the DMVs is enough to determine what may be ailing a SQL
Server machine.
An extensive number of DMVs are available in SQL Server 2008. Some DMVs are scoped at
the server level, and others are scoped at the database level. They are all found in the sys
schema and have names that start with dm_. Table 7.3 lists the different types of DMVs.
The DMVs in this table are categorized based on function as well as the starting characters
in the DMV names. The naming convention gives you an easy means for identifying the
type of each DMV.
TABLE 7.3 Types of DMVs
Category Name Prefix Information Captured
Auditing
dm_audit
New Auditing information
Service
Broker
dm_broker
Server Broker statistics, including activated tasks and connections
Change Data
dm_cdc
New Change Data Capture information
CLR
dm_clr
CLR information, including the CLR loaded assemblies
Cryptographic
dm_cryp
Security related data
TDE
dm_database
ptg
180
CHAPTER 7 SQL Server System and Database Administration
TABLE 7.3 Types of DMVs
Category Name Prefix Information Captured
Server
dm_server
Server Audit status
Transaction
dm_tran
Transactions and isolati on-leve l information
Object
dm_sql
Object References
Extended
Events
dm_xe
New event handling infrastructure
TIP
You can expand the Views node in a given database in the Object Explorer and open
the System Views node to see a list of the available DMVs. The DMVs are all listed
together and start with dm_. If you expand the Column node under each DMV, you see
the available columns to select from the view. You can then drag the column into a
query window to be included in a SELECT statement.
To illustrate the value of the DMVs, let’s look at a performance scenario and compare the SQL
Server 2000 approach to a SQL Server 2008 approach using DMVs. A common performance-
related question is “What stored procedures are executing most frequently on my server?”
With SQL Server 2000, the most likely way to find out is to run a Profiler trace. You must have
a Profiler trace that has already been running to capture the stored procedure executions, or
you must create a new trace and run it for a period of time to answer the performance ques-
Explorer within the master database. If you select Function, System Functions, Table-
Valued Functions, you see the dynamic management functions listed at the top.
DMVs are also a great source of information that does not relate directly to performance.
For example, you can use the dm_os_sys_info DMV to gather important server informa-
tion, such as the number of CPUs, the amount of memory, and so on. The following
example demonstrates the use of the dm_os_sys_info DMV to return CPU and memory
information:
select cpu_count, hyperthread_ratio, physical_memory_in_bytes
from sys.dm_os_sys_info
/* Results from prior select
cpu_count hyperthread_ratio physical_memory_in_bytes
2 2 2146357248
*/
The cpu_count column returns the number of logical CPUs, hyperthread_ratio returns
the ratio between physical CPUs and logical CPUs, and the last column selected returns
the physical memory on the SQL Server machine.
System Stored Procedures
System stored procedures have been a favorite of SQL Server DBAs since the inception of
SQL Server. They provide a rich set of information that covers many different aspects of
SQL Server. They can return some of the same types of information as system views, but
they generally return a fixed set of information that cannot be modified as you can when
using a SELECT statement against the system views. That is not to say that they are not
valuable; they are valuable, and they are particularly useful for people who have been using
SQL Server for a long time. System stored procedures such as sp_who, sp_lock, and sp_help
are tools for a database professional that are as basic as a hammer is to a carpenter.
System stored procedures have names that start with sp_, and they are found in the sys
schema. They are global in scope, which allows you to execute them from any database,
Download from www.wowebook.com
ptg
by the built-in system procedures). For more information and tips on creating your own
system stored procedures, refer to Chapter 28, “Creating and Managing Stored
Procedures.”
System stored procedures are listed in the Object Explorer, in the Programmability node
within Stored Procedures and then System Stored Procedures. There are far too many
system stored procedures to list or discuss them all here. A quick check of the master
database lists more than 1,000 procedures. SQL Server Books Online provides detailed
help on these procedures, which it groups into 18 different categories.
Useful System Stored Procedures
You are likely to use only a handful of system stored procedures on a regular basis. What
procedures you use depends on the type of work you do with SQL Server and your capac-
ity to remember their names. Table 7.4 contains a sample set of system stored procedures
that you may find useful.
Download from www.wowebook.com
ptg
183
System Stored Procedures
7
Many of the administrative functions performed by SSMS can also be accomplished with
system stored procedures. Examples include procedures that start with sp_add and sp_delete,
which can be used to add and delete database objects. In addition, more than 90 system
stored procedures start with sp_help, which return help information on database objects.
TIP
You can use the sys.all_objects catalog view to search for available system stored
procedures. This catalog view lists objects that are schema scoped as well as system
objects. For example, the query SELECT * FROM sys.all_objects WHERE name LIKE
‘sp_help%’ returns all the system stored procedures that start with sp_help. You can
turn to Books Online for detailed help on any of the system stored procedures. Just
enter sp_ in the index search, and you see a list of them all.
Becoming familiar with some of the system stored procedures is well worth your while.