July 2008 | SansSQL

Wednesday, July 16, 2008

Delete from Registry using SQL

xp_regdeletekey and xp_regdeletevalue are the two undocumented stored procedures that helps in deleting values and keys from registry. These stored procedures should be used very vary carefully as there are chances of harming the system and system may crash.

xp_regdeletekey
This is an extended stored procedure that will delete an entire key from the registry.
EXEC xp_regdeletekey @rootkey,@key
Example:-
EXEC master..xp_regdeletekey @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE\Test'

xp_regdeletevalue

This is an extended stored procedure that will delete a particular value for a key in the registry.

EXEC xp_regdeletevalue @rootkey,@key,@value_name

Example:-

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

Registry writing and regisrty reading through SQL

In SQL server we have 2 undocumented stored procedures for reading from registry and for writing into registry. For reading from registry we use the xp_regread and for writing into registry we use xp_regwrite undocumneted extended stored procedures. These two SP`s can be found in master database of a particular server.

Usage :-
EXEC xp_regread @rootkey, @key,[@value_name],[@Value]
Example:-
EXEC master.dbo.xp_regread @rootkey='HKEY_LOCAL_MACHINE', @key= 'SOFTWARE\Microsoft\Microsoft SQLServer\80\Replication\Subscriptions\',
@value_name= 'SubscriberEncryptedPasswordBinary'

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

Undocumented stored procedure for retrieving SQL Agent properties

sp_get_sqlagent_properties is and undocumented stored procedure to retrive the SQL Agent properties of a particular server. This stored procedure can be found in msdb database.

Usage:
EXEC msdb..sp_get_sqlagent_properties

Undocumented stored procedure:: sp_MS_upd_sysobj_category

sp_MS_upd_sysobj_category is an undocumneted stored procedure which Enables or disables a special system mode where all newly created objects are automatically shown as system objects in enterprise manager. sp_MS_upd_sysobj_category accepts a parameter @Mode. @Mode can either be 1 or 2. Setting the value of @Mode to 1 enables this special system mode and setting it to 2, disables it. Among other things, sp_MS_upd_sysobj_category allows the creation of user-defined INFORMATION_SCHEMA views.

Usage:
EXEC master.dbo.sp_MS_upd_sysobj_category @Mode
@Mode = 1 or 2
1=Enable
2=Disable

Enable special system mode:
EXEC master.dbo.sp_MS_upd_sysobj_category 1

Disable special system mode:
EXEC master.dbo.sp_MS_upd_sysobj_category 2

Friday, July 11, 2008

UnDocumented stored proc to read the error log

EXEC sp_readerrorlog

sp_readerrorlog is an undocumented stored procedure which helps in reading the SQL server error log.

UnDocumented stored proc to find the primary key and foreign key Constraints defined on a table

EXEC sp_MStablekeys @TableName

sp_MStablekeys is an undocumented stored procedure to find all the primary keys and foreign keys defined on a particular table in SQL 2000 and SQL 2005.
This Stored procedure accepts a parameter @TableName and gives the primary keys and foreign keys defined on a that table.

UnDocumented stored proc to find the Check Constraints used in a table

EXEC sp_MStablechecks TableName

sp_MStablechecks is an undocumented Stored Procedure in SQL 2000 and SQL 2005 which helps in finding the various Check constraints used in a particular table. This stored proc accepts a parameter TableName and gives the various check constraints used in that table.

UnDocumented stored proc to find the Table references.

EXEC sp_MStablerefs Table_Name

sp_MStablerefs is an undocumented Stored procedure in SQL 2000 and SQL 2005 to find the referenced tables of the given table (Table name passed to the SP).

Tuesday, July 8, 2008

Stored Procedure to find Primary keys and foreign keys of a table.

Here are the Stored Procedures to find the Primary keys and Foreign keys of a table.

1. SP to find Primary keys on a particular table

EXEC sp_pkeys 'Table_Name'

2. SP to find Foreign keys on a particular table

EXEC sp_fkeys 'Table_Name'

Tuesday, July 1, 2008

Recovery model and status of all databases

SELECT name,
DATABASEPROPERTYEX(name, 'Recovery') as [Recovery Model],
DATABASEPROPERTYEX(name, 'Status') as Status
FROM master.dbo.sysdatabases
ORDER BY 1

Use this query to get the recovery model and status of all the databases present in the server.

OR

EXEC sp_msforeachdb 'Select databasepropertyex(''?'', ''recovery'')as ''Recovery Model of ? Database'''

To find only the recovery model of all the databases.