Microsoft included several hundred stored procedures in the various versions of Microsoft SQL Server and it has documen Ted a good percentage of them. But Many stored procedures remain undocumented. Some are used within the Enterprise Manager GUI in SQL and were not intended to is used by other processes. Microsoft has slated some of stored procedures to is removed (or they have been removed) from future versions of SQL Server. While these stored procedures can is very useful and save you lots of the time, they can is changed at any time in their funct Ion or they can simply be removed.
The chart below shows that while many of the procedures have been carried from one version of Microsoft SQL Server To another, new stored procedures have been introduced, and some have been the removed. Most, if not all, of the "procedures require", the user to being a member of the sysadmin fixed server, The procedures. The stored procedures that interact and the file system also require that the user executing the procedure (as OK as SQ L Server ' s service Account] have access to the File/folder.
Procedure Name |
SQL 2000 |
SQL 2005 |
SQL 2008 |
Sp_executeresultset |
X |
|
|
Sp_msforeachdb |
X |
X |
X |
Sp_msforeachtable |
X |
X |
X |
Sp_readerrorlog |
X |
X |
X |
Xp_create_subdir |
|
X |
X |
Xp_delete_file |
|
X |
X |
Xp_dirtree |
X |
X |
X |
Xp_fileexist |
X |
X |
X |
Xp_fixeddrives |
X |
X |
X |
Xp_getfiledetails |
X |
|
|
Xp_getnetname |
X |
X |
X |
Xp_loginconfig |
X |
X |
X |
Xp_makecab |
X |
|
|
xp_msver |
X |
X |
X |
Xp_get_mapi_profiles |
X |
X |
X |
Xp_subdirs |
X |
X |
X |
Xp_test_mapi_profile |
X |
X |
X |
Xp_unpackcab |
X |
|
|
Sp_executeresultset
Microsoft removed this handy little procedure called Sp_executeresultset to SQL Server in SQL Server 2005. It allows to generate dynamic SQL code in the fly by using a SELECT query. Then, the resulting SQL commands would be executed against the database. It permits you into create a single piece of code so can, in a single step, find the number of records in every table in Y Our database (as the example shows). This is a undocumented stored procedure and there is no way to knowing it was why. But, alas, this handy utility is gone.
exec sp_execresultset ' select ' ' select ' ' ' + name + ' ',
Count (*) from ' + Name
From sysobjects
where xtype = ' U '
Sp_msforeachdb/sp_msforeachtable
Two procedures, SP_MSFOREACHDB and sp_msforeachtable, are wrappers, around a cursor. They allow to execute T-SQL code against each database on your SQL Server and each table within the current database, respectively. You are cannot, however, use a sp_msforeachtable command within a SP_MSFOREACHDB command in SQL and prior. The cursor name that is used within those procedures is the same (Hcforeach) and would therefore return an error saying That's the cursor name is already in a for each execution of the sp_msforeachtable. In SQL Server, Microsoft resolved this issue. In order to "next" command, your must tell one of the procedures it would be using a different replacement character oth Er than the default question mark. I Change the replacement character in the database command because it ' s easier.
Print each table name in the current database.
exec sp_msforeachtable ' print '? '
Print each database on the current server.
exec sp_msforeachdb ' print '? '
Print each table in the current server.
exec sp_msforeachdb ' use [@] exec sp_msforeachtable ' Print
''''@.?''''''', '@'
Sp_readerrorlog/xp_readerrorlog
The stored procedure Sp_readerrorlog actually comes in two forms. Each works the same; One is simply a wrapper for the second. The wrapper stored procedure is sp_readerrorlog and it calls Xp_readerrorlog. Both have four input parameters, but only the the ' the ' the ' the ' the ' two to us. The "parameter" establishes the "file number" you wish to view. The second is the "Log to View" (1 or null for ERRORLOG, 2 for SQL Agent log). This allows your to view your error logs quickly and easily instead of have to look at the bloated log viewer so now Co MES with SQL Server and SQL 2008.
View the current SQL ERRORLOG file.
EXEC Sp_readerrorlog
EXEC Sp_readerrorlog 0, 1
View the Prior SQL Agent Log file.
EXEC Sp_readerrorlog 1, 2
Xp_create_subdir
Introduced in SQL Server-Xp_create_subdir stored procedure is very handy because your can use it to create folder s on SQL Server ' s hard drive or on a network share from within T-SQL.
exec xp_create_subdir ' c:\MSSQL\Data '
Xp_delete_file
Use the Xp_delete_file stored procedure introduced in SQL Server, and delete files from SQL Server ' hard drive or a NE Twork share from within T-SQL.
Xp_dirtree
The Xp_dirtree procedure allows you to view the folder tree and/or file list beneath a folder. This procedure has several parameters so control how deep the procedure searches and whether it returns files and folder s or folders only. The parameter establishes the folder to look in. (Recommendation:do not run this procedure against the root of the Drive that Windows are installed on because it'll take some time to generate the "tree" and return the data. The second parameter limits the number of recursive levels that procedure would dig. The default is zero or all levels. The third parameter tells the procedure to include files. The default is zero or folders only, a value of 1 includes files in the result set. Specifying a third value not equal to zero would add a additional column to the output called file which is a bit field sh Owing the entry in a folder or file.
Get the full directory tree.
exec xp_dirtree ' d:\mssql\ '
Get the "the" two levels of the directory tree.
exec xp_dirtree ' d:\mssql\ ', 2
Get the "the" three levels of the directory tree, including files.
exec xp_dirtree ' d:\mssql\ ', 3, 1
Xp_fileexist
This SQL Server stored procedure, xp_fileexist, are used to determine if a file exists on SQL Server ' s hard drive or on a n Etwork share. It is extremely useful in stored procedures this load data from flat files. It allows to check and-if the file exists before attempting to blindly load the file. The procedure has two parameters. Use the "parameter to determine if" file or folder you want exists. The second is a output parameter, which when specified, returns a 1 or 0 if the file exists or does not.
Without the parameter.
exec xp_fileexist ' c:\importfile.csv '
With the parameter.
DECLARE @file_exists int
exec xp_fileexist ' c:\importfile.csv ', @file_exists OUTPUT
SELECT @file_exists
Xp_fixeddrives
The procedure xp_fixeddrives is one of the most useful procedures. It presents a list of all drive letters and the amount for free spaces each drive has. The parameter has a single optional input parameter This can filter the results by drive type. A value of 3 would return to all mass storage devices (CD-ROM, DVD, etc.); A value of 4 would return the hard drives; While a value of 2 would return removable media (USB thumb drives, flash drives, etc.).
return all drives.
EXEC xp_fixeddrives
return hard drives only.
EXEC xp_fixeddrives 2
Xp_getfiledetails
The procedure xp_getfiledetails is another extremely useful procedure, which am last available in SQL Server 2000. This procedure returns size, date and attributes information about the file specified, including date and times created, AC Cessed and modified.
exec xp_getfiledetails ' c:\filetoload.csv '
Xp_getnetname
The procedure Xp_getnetname returns the name of the physical machine where Microsoft SQL Server is installed. You can have the machine name returned as a record set or as a variable.
Without the parameter.
EXEC xp_getnetname
Using the parameter.
DECLARE @machinename sysname
EXEC xp_getnetname @machinename OUTPUT
Select @machinename
Xp_loginconfig
This SQL Server stored procedure'll tell you some basic authentication information about the user executing it. It tells the authentication method (Windows versus SQL Login), the default domain of the server, the audit level, as W ell as some internal separator information.
EXEC xp_loginconfig
Xp_makecab
Back in SQL Server ability, Microsoft gave us-compress OS files directly from T-SQL without have to shell Out to DOS via xp_cmdshell and run Third-party software, like PKZip or WinZip. That command is Xp_makecab. It allows to specify a list of files of your want to compress as as as as as the CAB file for you want to put them in. It even lets you select default compression, Mszip compression (akin to the. zip file format) or no compression. The parameter gives the path to the CAB file in which for you want to create or add files to. The second parameter is the compression level. The third parameter applies if you are want to use verbose logging. Starting with the fourth parameter and in down are the names of the the files for you want to compress. In my testing, I am able to pass file names to being compressed to the extended stored, procedure which A very flexible solution to your data compression requirements.
exec xp_makecab ' c:\test.cab ', ' Mszip ', 1, ' c:\test.txt ', ' c:\test1.txt '
xp_msver
The procedure xp_msver is very useful when looking for system information. It returns a wealth of information about the host operating system--the SQL version number, language, CPU type, Copyrigh T and trademark information, Microsoft Windows version, CPU count and affinity settings, physical memory settings and your Product Key. This is procedure has many input parameters that allow for you to filter down the records that are. Each parameter is a sysname data type, which accepts the name of one of the records. If any parameters are specified, only the rows specified as a parameter are.
NO filter specified.
EXEC xp_msver
Return only Platform and Comments records.
exec xp_msver ' Platform ', ' Comments '
Xp_get_mapi_profiles
The Xp_get_mapi_profiles procedure assists in configuring SQL Mail. When executed, it'll call to Windows via the SQL Mail component of SQL Server and display a list of available MAPI ProFi Les that are configured in Outlook and it specifies which profile are the default profile. If it doesn ' t display any records, then either Outlook isn't configured correctly or SQL Server is not running under a Do Main account with Outlook profiles configured. In order for this procedure in SQL Server/SQL Server 2008, you are must enable the "SQL Mail XPs" option in the Sur Face Area Configuration tool or within the sp_configure procedure.
EXEC xp_get_mapi_profiles
Xp_subdirs
The Xp_subdirs procedure displays a subset the information avaialble. Xp_subdirs'll display the subfolders in a given folder. It can be very handy when your are building a directory tree within a table dynamically and don't want to worry about The extra parameters of the Xp_dirtree procedure.
exec xp_subdirs ' d:\mssql '
Xp_test_mapi_profiles
The procedure xp_test_mapi_profiles is another undocumented stored procedure this is very useful if you are setting up S QL Mail. It'll start, then stop, a MAPI session to ensure so MAPI is configured correctly and working within the confines of Mi Crosoft SQL Server. I should note this is it does not verify the mail server configuration within the MAPI client (Outlook) nor does it send a te St message.
The procedure accepts a single input parameter. That parameter are the name of the MAPI profile you are wish to test. Like the xp_get_mapi_profiles procedure, with this stored procedure to function in SQL Server and SQL Server 2008, Must enable the "SQL Mail XPs" option in the Surface area Configuration tool or within the sp_configure procedure.
When working with the SQL Mail stored procedures, being aware that SQL Mail are still slated for removal to the Microsoft SQ L Server platform. That means the procedures sp_get_mapi_profiles and Xp_test_mapi_profiles are slated for removal, as they are part of the QL Mail subsystem. Work on SQL Server should and later using Database mail instead of SQL Mail to ensure code portabilit Y with future versions of SQL Server. Microsoft initially slated SQL Mail for removal in SQL Server 2008, however, based on it inclusion in the current beta re Lease, its future in SQL Server 2008 is unknown.
Xp_unpackcab
Along with the Xp_makecab procedure comes xp_unpackcab extended, stored procedure does it just ex Tracts files from CAB files. The the paramater is the CAB file, the second are the path to extract to and the third are verbose logging. A Fourth paramater lets you specify the ' extract to ' file name.
exec xp_unpackcab ' c:\test.cab ', ' c:\temp\ ', 1
While this isn't intended to being a complete list of the undocumented stored procedures in SQL Server, it does provide a re Ference Point for many of this procedures with the hope of making the lives of the SQL Server administrators. Remember, you should never count on this procedures surviving from one SQL Server version to next, nor should your exp ECT their code base to remain the same between versions. That's said, go code and enjoy.
All information provided about Microsoft SQL Server 2008 (Katmai) are based on Beta edition 10.0.1019 of the Software and is subject to change without notice.