SQL Server 6.5 Useful Undocumented Stored Procedures


Introduction

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 recreate the index.

See this link for more information:
“How can I fix a corruption in a system table?”
http://www.windows2000faq.com/Articles/Index.cfm?ArticleID=14051

This is the example:


EXEC sp_fixindex pubs, sysindexes, 2

sp_lock2


As a SQL Server DBA, I often need a reference to information about
locks. Microsoft recommends to use sp_lock system stored procedure
to report locks information. This very useful procedure returns the
information about SQL Server process ID, which lock the data, about
locked database, about locked table_id, about locked page, and about
type of locking.

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 provides an enhanced version of the sp_lock system stored
procedure, which returns TableName and Owner also. This is 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 as sp_who, but provides more detailed information.
sp_who2 returns CPUTime, DiskIO, LastBatch and ProgramName in addition
to sp_who.

Syntax


sp_who [login_id | ‘spid’]

where
login_id – the user’s login ID. If not specified, the procedure
reports on all 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 get the total size and the space
used by the tempdb database. You should execute sp_tempdbspace without
parameters.

This is the example:


EXEC sp_tempdbspace

Here is the result set 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 @tablename parameter to work with sp_MShelpcolumns.
To get the full columns 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 segment for the given
table.
You should specify @tablename parameter to work with sp_MShelpindex.
To get 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 many useful information about system
data types and about user data types.
You can specify @typename parameter to work with sp_MShelpindex,
or you can run sp_MShelpindex stored procedure without parameters.
To get 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 space
the table and index use.
You should specify @name parameter to work with sp_MShelpindex.

To determine the space used by the author table in the pubs database, run:


USE pubs
GO
EXEC sp_MStablespace ‘authors’
GO

Here is the result set 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 set 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 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 set 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 the
database. You can make cursor for this purpose, but you can also use
sp_MSforeachtable stored procedure in this case.

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 hard work.


EXEC sp_MSforeachtable @command1=”print ‘?’ DBCC DBREINDEX (‘?’)”

sp_MSkilldb


This stored procedure sets database to suspect and let 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



»


See All Articles by Columnist
Alexander Chigrik

Alexander Chigrik
Alexander Chigrik
I am the owner of MSSQLCity.Com - a site dedicated to providing useful information for IT professionals using Microsoft SQL Server. This site contains SQL Server Articles, FAQs, Scripts, Tips and Test Exams.

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles