Some SQL Server Stored Procedure parameters and Examples

Source: Internet
Author: User
Tags sql 2008 all mail

Microsoft authorized ded several hundred stored procedures in the varous versions of Microsoft SQL Server and it has provided ented a good percentage of them. but stored procedures remain uninitialized ented. some are used within the Enterprise Manager GUI in SQL 2000 and were not intended to be used by other processes. microsoft has slated some of these stored procedures to be removed (or they have been removed) from future versions of SQL Server. while these stored procedures can be very useful and save you lots of time, they can be changed at any time in their function or they can simply be removed.

The chart below shows that while listing of the procedures have been carried through from one version of Microsoft SQL Server to another, new stored procedures have been introduced, and some have been removed from the install package. most, if not all, of the procedures require the user to be a member of the sysadmin fixed server role in order to execute the procedures. the stored procedures that interact with the file system also require that the user executing the procedure (as well as SQL Server's service account) have access to the file/folder.

Procedure Name Sqls 2000 Sqls 2005 Sqls 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    


Microsoft removed this handy little procedure called sp_executeresultset from SQL Server in SQL Server 2005. it allows you to generate dynamic SQL code on the fly by using a SELECT query. then, the resulting SQL commands will be executed against the database. it permits you to create a single piece of code that can, in a single step, find the number of records in every table in your database (as the example shows ). this is an unrecoverable ented stored procedure and there is no way of knowing why it was removed. but, alas, this handy utility is gone.

Exec sp_execresultset 'select' SELECT ''' + name + '''''',
Count (*) FROM ''+ name
From sysobjects
Where xtype = ''u '''


Two procedures, sp_MSforeachdb and sp_MSforeachtable, are wrappers around a cursor. they allow you to execute T-SQL code against each database on your SQL Server and each table within the current database, respectively. you cannot, however, use an sp_MSforeachtable command within an sp_MSforeachdb command in SQL 2000 and prior. the cursor name that was used within those procedures was the same (hCForEach) and wocould therefore return an error saying that the cursor name was already in use for each execution of the sp_MSforeachtable. in SQL Server 2005, Microsoft resolved this issue. in order to "next" the command, you must tell one of the procedures it will be using a different replacement character other 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 on the current server.

Exec sp_MSforeachdb 'use [@] exec sp_MSforeachtable ''print
''''@.? ''''''','@'


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 CILS xp_readerrorlog. both have four input parameters, but only the first two are useful to us. the first parameter establishes the file number that you wish to view. the second is the log to view (1 or null for ERRORLOG, 2 for SQL Agent Log ). this allows you to view your error logs quickly and easily instead of having to look at the bloated log viewer that now comes with SQL Server 2005 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


Introduced in SQL Server 2005, the xp_create_subdir stored procedure is very handy because you can use it to create folders on SQL Server's hard drive or on a network share from within T-SQL.

Exec xp_create_subdir 'C: \ MSSQL \ data'


Use the xp_delete_file stored procedure introduced in SQL Server 2005 to delete files from SQL Server's hard drive or a network share from within T-SQL.


The xp_dirtree procedure allows you to view the folder tree and/or file list beneath a folder. this procedure has several parameters that control how deep the procedure searches and whether it returns files and folders or folders only. the first parameter establishes the folder to look in. (Recommendation: Do not run this procedure against the root of the drive that Windows is installed on because it will take some time to generate the tree and return the data .) the second parameter limits the number of recursive levels that the procedure will dig through. 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 pair des files in the result set. specifying a third value not equal to zero will add an additional column to the output called file which is a bit field showing the entry in a folder or file.

Get the full directory tree.

Exec xp_dirtree 'd: \ mssql \'

Get the first two levels of the directory tree.

Exec xp_dirtree 'd: \ mssql \ ', 2

Get the first three levels of the directory tree, including files.

Exec xp_dirtree 'd: \ mssql \ ', 3, 1


This SQL Server stored procedure, xp_fileexist, is used to determine if a file exists on SQL Server's hard drive or on a network share. it is extremely useful in stored procedures that load data from flat files. it allows you to check and see if the file exists before attempting to blindly load the file. the procedure has two parameters. use the first parameter to determine if the file or folder you want exists. the second is an 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


The procedure xp_fixeddrives is one of the most useful procedures. it presents a list of all drive letters and the amount of free space each drive has. the parameter has a single optional input parameter that can filter the results by drive type. A value of 3 will return all mass storage devices (CD-ROM, DVD, etc .); a value of 4 will return the hard drives; while a value of 2 will return removable media (USB thumb drives, flash drives, etc .).

Return all drives.

Exec xp_fixeddrives

Return hard drives only.

Exec xp_fixeddrives 2


The procedure xp_getfiledetails is another extremely useful procedure, which was last available in SQL Server 2000. this procedure returns size, date and attribute information about the file specified, including date and times created, accessed and modified.

Exec xp_getfiledetails 'C: \ filetoload.csv'


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


This SQL Server stored procedure will tell you some basic authentication information about the user executing it. it tells you the authentication method (Windows versus SQL Login), the default domain of the server, the audit level, as well as some internal separator information.

Exec xp_loginconfig


Back in SQL Server 2000, Microsoft gave us the ability to compress OS files directly from T-SQL without having to shell out to DOS via xp_{shell and run third-party software, like pkzip or winzip. that command was xp_makecab. it allows you to specify a list of files you want to compress as well as the cab file you want to put them in. it even lets you select default compression, MSZIP compression (akin to. zip file format) or no compression. the first parameter gives the path to the cab file in which you want to create or add files. the second parameter is the compression level. the third parameter applies if you want to use verbose logging. starting with the fourth parameter and on down are the names of the files you want to compress. in my testing, I was able to pass 45 file names to be compressed to the extended stored procedure, which means that it is a very flexible solution to your data compression requirements.

Exec xp_makecab 'C: \ test. cab', 'mszip ', 1, 'c: \ test.txt', 'c: \ test1.txt'


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, copyright and trademark information, Microsoft Windows version, CPU count and affinity settings, physical memory settings and your product key. this procedure has wrote input parameters that allow you to filter down the records that are returned. 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 returned.

No filter specified.

Exec xp_msver

Return only Platform and Comments records.

Exec xp_msver 'Platform ', 'comments'


The xp_get_mapi_profiles procedure assists you in processing ing SQL Mail. when executed, it will call to Windows via the SQL Mail component of SQL Server and display a list of available MAPI profiles that are configured in Outlook and it specifies which profile is the default profile. if it doesn' t display any records, then either Outlook is not configured correctly or SQL Server is not running under a domain account with Outlook profiles configured. in order to use this procedure in SQL Server 2005 or SQL Server 2008, you must enable the "SQL Mail XPs" option in the Surface Area Configuration tool or within the sp_configure procedure.

Exec xp_get_mapi_profiles


The xp_subdirs procedure displays a subset of the information avaialble through xp_dirtree. xp_subdirs will display all the subfolders in a given folder. it can be very handy when you are building a directory tree within a table dynamically and you do not want to worry about the extra parameters of the xp_dirtree procedure.

Exec xp_subdirs 'd: \ mssql'


The procedure xp_test_mapi_profiles is another unencrypted ented stored procedure that is very useful when you are setting up SQL Mail. it will start, then stop, a MAPI session to ensure that MAPI is configured correctly and working within the confines of Microsoft SQL Server. I shoshould note that it does not verify the mail server configuration within the MAPI client (Outlook) nor does it sends a test message.

The procedure accepts a single input parameter. that parameter is the name of the MAPI profile you wish to test. like the xp_get_mapi_profiles procedure, for this stored procedure to function in SQL Server 2005 and SQL Server 2008, you 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, be aware that SQL Mail is still slated for removal from the Microsoft SQL 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 SQL Mail subsystem. you shoshould do all mail work on SQL Server 2005 and later using Database Mail instead of SQL Mail to ensure code portability with future versions of SQL Server. microsoft initially slated SQL Mail for removal in SQL Server 2008, however, based on its release sion in the current beta release, its future in SQL Server 2008 is unknown.


Along with the xp_makecab procedure comes the xp_unpackcab extended stored procedure, and it does just what it says: It extracts files from cab files. the first paramater is the cab file, the second is the path you want to extract to and the third is verbose logging. A fourth paramater lets you specify the "extract to" file name.

Exec xp_unpackcab 'C: \ test. cab', 'c: \ temp \ ', 1

While this is not intended to be a complete list of the unencrypted ented stored procedures in SQL Server, it does provide a reference point for processing of these procedures with the hope of making the lives of the SQL Server administrators easier. remember, you shocould never count on these procedures into ving from one SQL Server version to the next, nor shocould you perform CT their code base to remain the same between versions. that said, go code and enjoy.

All information provided about Microsoft SQL Server 2008 (Katmai) is based on beta edition 10.0.1019 of the software and is subject to change without notice.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.