Monday 17 September 2012

Extended Stored Procedures

      An extended stored procedure (xp) is a dynamic link library that runs directly in the address space of SQL Server. You can run extended stored procedures as normal stored procedures. Extended stored procedures are used to extend the capabilities of SQL Server. You can take advantage of the many extended stored procedures that come with SQL Server, or you can write your own.

This article describes some useful undocumented extended stored procedures that
shipped with SQL Server 2005.

1) SP_MSgetversion
This extended stored procedure can be used to get the current version of Microsoft SQL Server.
To get the current SQL Server version, run:

EXEC master..sp_MSgetversion


Note. A more common way to retrieve the current SQL Server version (this way provides more
information) is to use following SELECT statement:

SELECT @@version




2) XP_availablemedia
This extended stored procedure returns available drives and free space in bytes on these drives.
In comparison with xp_fixeddrives the xp_availablemedia extended stored procedure returns not
only the hard drives, but all available drives.
The xp_availablemedia returns free space in bytes when xp_fixeddrives returns free space in Mb.
To get the list of all available drives with free space on them, run:

EXEC master..xp_availablemedia




3) XP_delete_file
This extended stored procedure can be used to delete a SQL Server backup file or a Maintenance
Plan report file.

Syntax:
EXECUTE xp_delete_file 0|1, 'file_name'

0 - to delete a SQL Server backup file.
1 - to delete a Maintenance Plan report file.


For example, to delete the TestBackup.bak SQL Server backup file from the SQL directory of the
D: disk, you can run this statement:

EXEC master..xp_delete_file 0, 'd:\SQL\TestBackup.bak'




4) XP_dirtree
This extended stored procedure can be used to get a list of all the folders for the folder named
in the xp. To get a list of all the folders in the D:\Install folder, run:

EXEC master..xp_dirtree 'D:\Install'




5) XP_create_subdir
This extended stored procedure creates a subdirectory for the specified directory. For example,
to create 'SQL' subdirectory in the 'Install' directory on the D: disk, you can run the following:

EXEC master..xp_create_subdir 'D:\Install\SQL'




6) XP_enum_oledb_providers
This extended stored procedure is used to list of all the available OLE DB providers. It returns
Provider Name, Parse Name and Provider Description. To get a list of all OLE DB providers for your
SQL Server, run:

EXEC master..xp_enum_oledb_providers




7) XP_enumcodepages
This extended stored procedure can be used to list of all code pages, character sets and their
description for your SQL Server. To get a list of all code pages and character sets, run:

EXEC master..xp_enumcodepages


Note. To run this procedure, you should enable the 'Web Assistant Procedures' using the sp_configure
system stored procedure.


8) XP_enumerrorlogs
This extended stored procedure returns the list of all error logs with their last change date
and error log files size.
To get the list of error logs, run:

EXEC master..xp_enumerrorlogs




9) XP_fileexist
You can use this extended stored procedure to determine whether a particular file exists on the
disk or not.

Syntax:

EXECUTE xp_fileexist filename [, file_exists INT OUTPUT]


For example, to check whether the file boot.ini exists on C: disk  or not, run:

EXEC master..xp_fileexist 'c:\boot.ini'




10) XP_fixeddrives
This very useful extended stored procedure returns the list of all hard drives and the amount of
free space in Mb for each hard drive.

To see the list of drives, run:

EXEC master..xp_fixeddrives




11) XP_getnetname
This extended stored procedure returns the WINS name of the SQL Server that you are connected
to. To view the SQL Server name, run:

EXEC master..xp_getnetname




12) XP_get_tape_devices
This extended stored procedure is used to get the names of all the available tape devices.
To get a list of all tape devices for your SQL Server, run:

EXEC master..xp_get_tape_devices




13) XP_readerrorlog
This extended stored procedure returns the content of the last errorlog file. To see the text
of the errorlog file, run:

EXEC master..xp_readerrorlog




14) XP_regdeletekey
This extended stored procedure can be used to delete an entire key from the registry. You should
use it very carefully.

Syntax:

EXECUTE xp_regdeletekey [@rootkey=]'rootkey',
                        [@key=]'key'


For example, to delete the 'SOFTWARE\Test' key from 'HKEY_LOCAL_MACHINE', run:

EXEC master..xp_regdeletekey

     @rootkey='HKEY_LOCAL_MACHINE', 
     @key='SOFTWARE\Test'




15) XP_regdeletevalue
This extended stored procedure can be used to delete a particular value for a key in the registry.
You should use it very carefully.

Syntax:

EXECUTE xp_regdeletevalue [@rootkey=]'rootkey',
                          [@key=]'key',
                          [@value_name=]'value_name'


For example, to delete the 'TestValue' value for the 'SOFTWARE\Test' key from
'HKEY_LOCAL_MACHINE', run:

EXEC master..xp_regdeletevalue
     @rootkey='HKEY_LOCAL_MACHINE',
     @key='SOFTWARE\Test',
     @value_name='TestValue'




16) XP_regread
This extended stored procedure is used to read from the registry.

Syntax:

EXECUTE xp_regread [@rootkey=]'rootkey',
                   [@key=]'key'
                   [, [@value_name=]'value_name']
                   [, [@value=]@value OUTPUT]


For example, to read into the @test variable from the 'TestValue' value from the 'SOFTWARE\Test'
key from the 'HKEY_LOCAL_MACHINE', run:

DECLARE @test varchar(20)
EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
  @key='SOFTWARE\Test',
  @value_name='TestValue',
  @value=@test OUTPUT
SELECT @test




17) XP_regwrite
This extended stored procedure is used to write to the registry.

Syntax:

EXECUTE xp_regwrite [@rootkey=]'rootkey',
                    [@key=]'key',
                    [@value_name=]'value_name',
                    [@type=]'type',
                    [@value=]'value'


For example, to write the 'Test' variable to the 'TestValue' value, 'SOFTWARE\Test' key,
'HKEY_LOCAL_MACHINE', run:

EXEC master..xp_regwrite
     @rootkey='HKEY_LOCAL_MACHINE',
     @key='SOFTWARE\Test',
     @value_name='TestValue',
     @type='REG_SZ',
     @value='Test'




18) XP_test_mapi_profile
This extended stored procedure can be used to resolve problems with mapi profiles. For example,
you can run the statement below to test if the 'profile_name' mapi profile exist and correctly
configured:

EXEC master..xp_test_mapi_profile 'profile_name'







Reference: Govind Badkur(http://govindbadkur.blogspot.com)

No comments:

Post a Comment