A system stored procedure is a stored
procedure that has some special characteristics. These procedures,
created when SQL Server is installed or upgraded, are generally used to
administer SQL Server. They shield a DBA from accessing the system
catalogs directly. Some system stored procedures are used to present
information from the system catalog, and others modify the system
catalogs.
Note
System stored procedures seem to have fallen out of
favor with Microsoft. Most of them have been listed as deprecated
features in SQL Server 2008 and been replaced with T-SQL commands, or
the information provided by system stored procedures is now available
via the catalog views and dynamic management views. Many of the current
system stored procedures may be removed in future versions of Microsoft
SQL Server, so it is recommended that you avoid using many of the system
stored procedures in any of your development work and modify any code
currently using system stored procedures to use the alternatives.
Although many of the system stored procedures have
been deprecated and are not as critical to administering SQL Server as
they once were, it is still a good idea to be familiar with the basic
system stored procedures. There are currently around 400 documented
system stored procedures in SQL Server 2008, so it would be a tough job
to learn the names and syntax for all of them. The total number of
system stored procedures is more than 1,400. Some of the undocumented
stored procedures are called by other procedures, and others are called
from SSMS or other SQL Server tools and utility programs.
The following attributes characterize a system stored procedure:
- The stored procedure name begins with sp_.
- The stored procedure resides in the Resource database.
- The procedure is defined in the sys schema.
These attributes make the procedure global,
which means you can execute the procedure from any database without
qualifying the database name. The procedure executes within the current
database context.
Although system stored procedures reside in the Resource
database, they also run in any database context when fully qualified
with a database name, regardless of the current database context. For
instance, sp_helpfile shows information about the files configured for the current database. In the following example, when not qualified, sp_helpfile returns file information for the master database, and when qualified with bigpubs2008.., it returns file information for the bigpubs2008 database:
exec sp_helpfile
go
name fileid filename filegroup
size maxsize growth usage
-------- ------ --------------------------------------------------------- --------- --------- --------- ------ -----------
master 1 C:\MSSQL2008\MSSQL10.SQL2008UNLEASHED\MSSQL\DATA\master.mdf PRIMARY
4096 KB Unlimited 10% data only
mastlog 2 C:\MSSQL2008\MSSQL10.SQL2008UNLEASHED\MSSQL\DATA\mastlog.ldf NULL
512 KB Unlimited 10% log only
exec bigpubs2008..sp_helpfile
go
name fileid filename filegroup size
maxsize growth usage
-----------------------------------------------------------------------------------
------------------------
bigpubs2008 1 E:\MSSQL2008\DATA\bigpubs2008.mdf PRIMARY 214912 KB
Unlimited 10% data only
bigpubs2008_log 2 E:\MSSQL2008\DATA\bigpubs2008_log.LDF NULL 504 KB
Unlimited 10% log only
Table 1 describes the categories of system stored procedures.
Table 1. System Stored Procedure Categories
Category | Description |
---|
Catalog stored procedures | Used to implement ODBC data dictionary functions and isolate ODBC applications from changes to underlying system tables. |
Cursor stored procedures | Used to implement cursor variable functionality. |
Database engine stored procedures | Used for general maintenance of the SQL Server Database Engine. |
Database mail stored procedures | Used to perform email operations from within an instance of SQL Server. |
Database maintenance plan procedures | Used to set up core database maintenance tasks. |
Distributed queries stored procedures | Used to link remote servers and manage distributed queries. |
Full-text search stored procedures | Used to implement and query full-text indexes. |
Log shipping stored procedures | Used to configure, modify, and monitor log shipping configurations. |
Automation stored procedures | Allow OLE automation objects to be used within a T-SQL batch. |
Notification services stored procedures | Used to manage SQL Server 2008 Notification Services. |
Replication stored procedures | Used to manage replication. |
Security stored procedures | Used to manage security, such as login IDs, usernames, and so on. |
SQL Server Profiler stored procedures | Used by SQL Server Profiler to monitor performance and activity. |
SQL Server Agent stored procedures | Used by SQL Server Agent to manage scheduled and event-driven activities. |
Web task stored procedures | Used for creating web pages. |
XML stored procedures | Used for XML text management. |
General extended stored procedures | Provide an interface from an instance of SQL Server to external programs for various maintenance activities (for example, xp_sqlmaint) |
Some of the more useful system stored procedures are listed in Table 2.
Table 2. Useful System Stored Procedures
Procedure Name | Description |
---|
sp_who and sp_who2 | Return information about current connections to SQL Server. |
sp_help [object_name] | Lists the objects in a database or returns information about a specified object. |
sp_helpdb | Returns a list of databases or information about a specified database. |
sp_configure | Lists or changes configuration settings. |