Documente Academic
Documente Profesional
Documente Cultură
DBCC Commands
Prepared By : Ramesh
Extended Stored Procedures
These are some documented and undocumented extended stored procedures. These procedures
expand the flexibility of T-SQL to allow you to interact with the Windows operating system.
Most of the procedures in this section are undocumented and are not supported by Microsoft.
xp_availablemedia
The xp_availablemedia extended stored procedure lists the drives that are available to read and
write data. To execute the procedure, use the following syntax:
master..xp_availablemedia
This outputs the name of the drive, the free space in bytes (shown as low free) and the type of
drive. Some of the types of drives you can see in the following results are a floppy drive (1), a
hard drive (2), and a writable CD-ROM (8):
xp_cmdshell
The xp_cmdshell extended stored procedure is a commonly used procedure to execute programs
using T-SQL. For example, you can use the procedure to execute a DTS package by using the
following syntax:
output
--------------------------------------------------
DTSRun: Loading...
DTSRun: Executing...
DTSRun OnStart: DTSStep_DTSExecuteSQLTask_1
DTSRun OnFinish: DTSStep_DTSExecuteSQLTask_1
DTSRun: Package execution complete.
NULL
If you don't want the procedure to produce any output, use the no_output parameter.
xp_dirtree
2
Prepared By : Ramesh
The xp_dirtree extended stored procedure reports all of the subdirectories under the specified
root directory. For example, if you'd like to determine which directories exist under the
C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP folder, use the following syntax:
This outputs all the directories under the specified directory as shown here:
subdirectory depth
-------------------------------------------------- -----------
Northwind 1
TranBackups 2
Pubs 1
The depth column shows how deep under the specified directory the directory named in the
subdirectory column is. If you don't want the depth column, use the xp_subdirs procedure with
the same parameters.
xp_enum_activescriptengines
The xp_enum_activescriptengines procedure lists all the scripting languages installed on the
server. To execute the procedure, use the following syntax:
master..xp_enum_activescriptengines
This outputs the following results, which may vary based on the installed languages:
Program ID Description
------------------------------ ------------------------------
XML XML Script Engine
VBScript VB Script Language
VBScript.Encode VBScript Language Encoding
JScript JScript Language
JScript.Encode JScript Language Encoding
xp_enum_oledb_providers
The xp_enum_oledb_providers procedure outputs all the installed OLE DB providers on the
server. To execute the procedure, use the following syntax:
master..xp_enum_oledb_providers
Prepared By : Ramesh
MSOLAP Microsoft OLE DB Provider for Olap Service
MSDASQL Microsoft OLE DB Provider for ODBC Driver
ADsDSOObject OLE DB Provider for Microsoft Directory
MSDAOSP Microsoft OLE DB Simple Provider
SQLOLEDB Microsoft OLE DB Provider for SQL Server
Microsoft.Jet.OLEDB.4.0 Microsoft Jet 4.0 OLE DB Provider
xp_enumcodepages
The xp_enumcodepages procedure lists all the code pages installed on the server. To execute
the procedure, use the following syntax:
master..xp_enumcodepages
xp_enumdsn
The xp_enumdsn procedure outputs a list of DSNs set up on the server. To execute the
procedure, use the following syntax:
master..xp_enumdsn
xp_enumerrorlogs
The xp_enumerrorlogs procedure outputs a list of the SQL Server error logs on the server,
along with their creation dates and sizes. To execute the procedure, use the following syntax:
Prepared By : Ramesh
master..xp_enumerrorlogs
xp_enumgroups
The xp_enumgroups procedure lists all the Windows local groups on the server.
To execute the procedure, use the following syntax:
master..xp_enumgroups
If you run this procedure on a server other than Windows NT or 2000, you receive the following
error:
If you have Windows NT or 2000, you receive the results similar to the following (mine are
limited to 50 characters for the comment):
group comment
----------------------- --------------------------------------------------
Administrators Administrators have complete and unrestricted acce
Backup Operators Backup Operators can override security restriction
Guests Guests have the same access as members of the User
Power Users Power Users possess most administrative powers wit
Replicator Supports file replication in a domain
Users Users are prevented from making accidental or inte
xp_fileexist
The xp_fileexist procedure determines if a specified file exists in the directory. It can also tell
you if the specified variable is a directory. You can use this to trigger certain events. For
example, you can use this procedure to test for the existence of a file before launching a DTS
package. To execute the procedure to test the existence of a file called autoexec.bat, use the
following syntax:
master..xp_fileexist 'c:\autoexec.bat'
This outputs the following results (a result of 1 means the file or directory exists):
5
Prepared By : Ramesh
File Exists File is a Directory Parent Directory Exists
----------- ------------------- -----------------------
1 0 1
xp_fixeddrives
The xp_fixeddrives procedure displays the fixed drives on the server and how much space in
megabytes is available to each drive. You can use this to determine if there is enough space on a
drive before creating a new database or launching a DTS package to load large amounts of data.
To execute the procedure, use the following syntax:
master..xp_fixeddrives
drive MB free
----- -----------
C 1773
D 1386
E 12429
F 29276
xp_getfiledetails
The xp_getfiledetails procedure displays information about the specified file, including its size
and creation date. To execute the procedure, use the following syntax:
master..xp_getfiledetails 'c:\autoexec.bat'
Alternate Name Size Creation Date Creation Time Last Written Date
--------------- ------ ------------- ------------- -----------------
(null) 221 19800101 0 19970327
Last Written Time Last Accessed Date Last Accessed Time Attributes
----------------- ------------------ ------------------ -----------
134752 19991007 0 128
xp_get_MAPI_profiles
The xp_get_MAPI_profiles procedure will return a list of MAPI profiles that are available to
SQL Mail. The procedure executes with no parameters:
master..xp_get_MAPI_profiles
If profiles exist, you will receive results that look something like this:
Prepared By : Ramesh
Profile name Is default profile
---------------------------------- ------------------
Microsoft Outlook Internet Setting 1
If there are no MAPI profiles installed, you will receive the following message:
L -28
xp_getnetname
The xp_getnetname procedure displays the computer name of the SQL Server you're connected
to. To execute the procedure, use the following syntax:
master..xp_getnetname
xp_loginconfig
The xp_loginconfig procedure lists the security and login configuration for SQL Server. To
utilize the procedure, use the following syntax:
master..xp_loginconfig
name config_value
---------------------------- ----------------------------
login mode Mixed
default login guest
default domain BEDROCK
audit level none
set hostname false
map _ domain separator
map $ NULL
map # -
Prepared By : Ramesh
You can also optionally list the individual configuration item if you only want selective
information:
xp_logevent
The xp_logevent procedure is a useful procedure that logs events to Event Viewer from T-SQL.
To use the procedure, you have to provide an error number, followed by the error message, and
finally the severity. Available severities are INFORMATIONAL, WARNING, and ERROR.
User-defined error numbers begin at 50,001. For example, you can use the following syntax to
log an event into Event Viewer:
xp_logininfo
The xp_logininfo extended stored procedure produces valuable information about which
Windows users have rights to your SQL Server and the types of permissions they have. To
execute the procedure, use the following base syntax:
master..xp_logininfo
You can also gain information about individual Windows users or groups by specifying an
individual user or group as a parameter:
master..xp_logininfo 'BUILTIN\Administrators'
xp_ntsec_enumdomains
The xp_ntsec_enumdomains procedure lists the domain that your Windows server is a member
of. To execute the procedure, use the following syntax:
master..xp_ntsec_enumdomains
Domain
---------------------------------
XANADU
8
Prepared By : Ramesh
xp_readerrorlog
The xp_readerrorlog procedure returns the contents of the current error log. To execute the
procedure, use the following syntax:
master..xp_readerrorlog
You can also pass the extended stored procedure a simple parameter of what error log you'd like
to read. This number corresponds with the filename. For example, to read the file errorlog.5, you
would use the following command:
master..xp_readerrorlog 5
xp_regdeletekey
The xp_regdeletekey procedure allows you to delete a key from your registry. Be especially
careful when using this procedure as it will not warn you before the procedure deletes the entire
key. To run the procedure, use the @rootkey parameter to specify the root registry key and then
the @key parameter to designate the key you want to delete:
master..xp_regdeletekey @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\NewKey'
This outputs the following error if the registry key can't be found:
xp_regdeletevalue
You can also delete individual data items inside a registry key by using the xp_regdeletevalue
procedure. The procedure uses the same parameters as the xp_regdeletekey procedure, but has
an added @value_name parameter, which is the data item name:
master..xp_regdeletevalue @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\NewKey',
@value_name='NewKeyName'
xp_regenumvalues
The xp_regenumvalues lists all the registry data items and values in a given key. To use the
procedure, use the following syntax:
master..xp_regenumvalues 'HKEY_LOCAL_MACHINE',
'SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\CurrentVersion\'
9
Prepared By : Ramesh
This results in the following:
xp_regread
The xp_regread procedure reads an individual registry key to determine its existence or to read
a data item in the key. The @rootkey parameter is the root key in the registry, and the @key
parameter is the individual key. To use the procedure, you must specify the registry key's root
and key as shown here:
master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\'
By using these two variables, you are only testing the existence of the key, which results in the
following:
KeyExist
-----------
1
You can also read individual data items inside the key by using the added @value_name
parameter, which represents the name of the data item:
master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\',@value_name='defaultLogin'
Value Data
------------------------------ ------------------------------
defaultLogin guest
xp_regwrite
The xp_regwrite procedure allows you to create a data item in your server's registry and
optionally create a new key. To use the procedure, you must specify the root key with the
@rootkey parameter and designate an individual key with the @key parameter. If the key doesn't
10
Prepared By : Ramesh
exist, it is created. The @value_name parameter designates the data item and the @type
parameter the type of the data item. Valid data item types include REG_SZ and REG_DWORD.
The final option is the @value parameter, which assigns a value to the data item.
The following syntax adds a new key called NewKey, and creates a new data item under it called
NewKeyName:
master..xp_regwrite @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\NewKey',
@value_name='NewKeyName',
@type='REG_SZ', @value='Test'
xp_subdirs
The xp_subdirs procedure displays all the subdirectories one level down from the specified
directory. To execute the procedure, simply pass it the root directory you'd like to see:
subdirectory
------------------------
80
MSSQL
11
Prepared By : Ramesh