MSSQLCity.Com - All about MS SQL
     
About Us  
SSWUG Articles  
Articles  
Administering  
Comparison  
General  
Know How  
Replication  
Tuning  
Undocumented  
UDF  
SQL 6.5  
FAQ  
Scripts  
Tips  
Test Exams  
Advertise  
Download  
History  
Search  
Traffic  
Related Links  
     
Your button logo
Add to Favorites
 
     
 

SQL Server 6.5 Useful undocumented stored procedures

Alexander Chigrik
chigrik@mssqlcity.com


In this article, I want to tell you about some useful undocumented stored procedures shipped with SQL Server 6.5.

sp_fixindex

This stored procedure can be used to fix a corruption in a system table by recreating the indexes on the system table.

See this link for more information:
How can I fix a corruption in a system table?

This is the example:

EXEC sp_fixindex pubs, sysindexes, 2

sp_lock2

As a SQL Server DBA, I often need information about locks. Microsoft recommends using the sp_lock system stored procedure to report locks information. This is a very useful procedure that returns useful information about the SQL Server process ID, the locktype, the table, and database name.

This is the result set of sp_lock stored procedure:

spid   locktype                            table_id    page        dbname
------ ----------------------------------- ----------- ----------- ---------------
12     Sh_intent                           688005482   0           master
12     Ex_extent                           0           336         tempdb
Microsoft also provides an enhanced version of the sp_lock system stored procedure, which returns TableName and Owner also. This is the sp_lock2 stored procedure.

This is the example:

EXEC sp_lock2

sp_who2

This stored procedure returns information about current SQL Server 6.5 users and processes similar to sp_who, but it provides more detailed information.
sp_who2 returns CPUTime, DiskIO, LastBatch and ProgramName, in addition to the information provided by sp_who.

Syntax:

sp_who [login_id | 'spid']

where

login_id - the user's login ID. If not specified, the procedure
           reports on all of the active users of SQL Server.
spid     - the specific process id.
This example returns information for the 'sa' login:

EXEC sp_who2 'sa'

sp_tempdbspace

This stored procedure can be used to find the total size and the space used by the tempdb database. You should execute the sp_tempdbspace without parameters.

This is the example:

EXEC sp_tempdbspace

Here is the result from my machine:

database_name database_size             spaceused
------------- ------------------------- ------------------
tempdb        2.000000                  0.640625

sp_MShelpcolumns

This stored procedure returns the complete schema for a table, including the length, type, name, and whether a column is computed. You should specify the @tablename parameter to work with the sp_MShelpcolumns. To return the full column description for the authors table in the pubs database, run:

USE pubs
GO
EXEC sp_MShelpcolumns 'authors'
GO

sp_MShelpindex

This stored procedure returns information about name, status, fill factor, index columns names, and about used segments for the given table. You should specify the @tablename parameter to work with the sp_MShelpindex. To return the indexes description for the authors table in the pubs database, run:

USE pubs
GO
EXEC sp_MShelpindex 'authors'
GO

sp_MShelptype

This stored procedure returns much useful information about system and user data types.
You can specify @typename parameter to work with sp_MShelpindex, or you can run sp_MShelpindex stored procedure without parameters. To return information about all built-in and user defined data types, run:

USE pubs
GO
EXEC sp_MShelptype
GO

sp_MStablespace

This stored procedure returns the number of rows and the amount of space the table and index use. You should specify @name parameter to work with the sp_MShelpindex.

To return the amount of space used by the authors table in the pubs database, run:

USE pubs
GO
EXEC sp_MStablespace 'authors'
GO
Here is the result from my machine:

Rows        DataSpaceUsed IndexSpaceUsed
----------- ------------- --------------
23          2             8

sp_MSindexspace

This stored procedure returns the size in kb, which the indexes in the particular table use.

Syntax:

sp_MSindexspace [@tablename, [@index_name]]

where

@tablename  - the table name.
@index_name - the index name.
This is the example:

USE pubs
GO
EXEC sp_MSindexspace 'authors'
GO
Here is the result from my machine:

Index ID Index Name                     Size (KB)   Comments
-------- ------------------------------ ----------- --------------------------
1        UPKCL_auidind                  4           Size excludes actual data.
2        aunmind                        4           (None)

sp_MStablerefs

This stored procedure, when used with the @tablename parameter, returns all the dependencies for any table.

To get all dependencies for the titleauthor table in the pubs database, run:

USE pubs
GO
EXEC sp_MStablerefs 'titleauthor'
GO
Here is the result from my machine:

candidate_table       candidate_key    referenced
--------------------- ---------------- ----------
dbo.authors           N/A              1
dbo.titles            N/A              1

sp_MSforeachtable

Sometimes, you need to perform the same actions for all tables in a database. You can create a cursor for this purpose, or you can also use the sp_MSforeachtable stored procedure.

For example, you can use this stored procedure to rebuild all indexes in your database. Try to schedule it to execute when your server is not very busy.

EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?')"

sp_MSkilldb

This stored procedure sets a database to suspect mode and uses DBCC DBREPAIR to kill it. You should run this sp from the context of the master database. Use it very carefully.

To kill the pubs database, run:

USE master
GO
EXEC sp_MSkilldb 'pubs'
GO

 

 
Visit The SQL Server Worldwide User's Group for all the latest news and information about SQL Server, Oracle, DB2 and XML for developers and administrators.

(c) 1997, 2005 Bits on the Wire, Inc