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 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
xp_create_subdir
Introduced in SQL Server 2005, the xp_create_subdir stored procedure is very handy because you can use it to create folders on SQL Servers hard drive or on a network share from within T-SQL.
exec xp_create_subdir c:MSSQLData
xp_delete_file
Use the xp_delete_file stored procedure introduced in SQL Server 2005 to delete files from SQL Servers hard drive or a network 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 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 includes 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
xp_fileexist
This SQL Server stored procedure, xp_fileexist, is used to determine if a file exists on SQL Servers 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
xp_fixeddrives
The procedure xp_fixeddrives is one of the most usef