Introduction
In this article, I want to tell you about some useful undocumented
stored procedures shipped with SQL Server 2000.
sp_MSget_qualified_name
This stored procedure is used to get the qualified name for the given
object id.
Syntax
sp_MSget_qualified_name object_id, qualified_name
where
- object_id
- is the object id. object_id is int.
- qualified_name
- is the qualified name of the object. qualified_name is nvarchar(512).
This is the example to get the qualified name for the authors table
from the pubs database.
USE pubsGO
declare @object_id int, @qualified_name nvarchar(512)
select @object_id = object_id('authors')
EXEC sp_MSget_qualified_name @object_id, @qualified_name output
select @qualified_name
GO
Here is the result set from my machine:
--------------------------------------[dbo].[authors]
sp_MSdrop_object
This stored procedure is used to drop the object (it can be table,
view, stored procedure or trigger) for the given object id, object
name, and object owner. If object id, object name, and object owner
are not specified, then nothing will be dropped.
Syntax
sp_MSdrop_object [object_id] [,object_name] [,object_owner]
where
- object_id
- is the object id. object_id is int,
with a default of NULL.
- object_name
- is the name of the object. object_name is sysname,
with a default of NULL.
- object_owner
- is the object owner. object_owner is sysname,
with a default of NULL.
This is the example to drop the titleauthor table from the
pubs database.
USE pubsGO
declare @object_id int
select @object_id = object_id('titleauthor')
EXEC sp_MSdrop_object @object_id
GO
sp_gettypestring
This stored procedure returns the type string for the given table id
and column id.
Syntax
sp_gettypestring tabid, colid, typestring
where
- tabid
- is the table id. tabid is int.
- colid
- is the column id. colid is int.
- typestring
- is the type string. It's output parameter.
typestring is nvarchar(255).
This is the example to get the type string for the column number 2
in the authors table, from the pubs database.
USE pubsGO
declare @tabid int, @typestring nvarchar(255)
select @tabid = object_id('authors')
EXEC sp_gettypestring @tabid, 2, @typestring output
select @typestring
GO
Here is the result set from my machine:
-------------------------------varchar(40)
sp_MSgettools_path
This stored procedure returns the path to the SQL Server 2000 tools
and utilities.
Syntax
sp_MSgettools_path install_path
where
- install_path
- is the installation path. It's output parameter.
install_path is nvarchar(260).
This is the example to get the path to the SQL Server 2000 tools
and utilities.
USE masterGO
declare @install_path NVARCHAR(260)
EXEC sp_MSgettools_path @install_path OUTPUT
select @install_path
GO
Here is the result set from my machine:
------------------------------------------------------------C:\Program Files\Microsoft SQL Server\80\Tools
sp_MScheck_uid_owns_anything
This stored procedure returns the list of the object, owned by the
specified user.
Syntax
sp_MScheck_uid_owns_anything uid
where
- uid
- is the User ID, unique in this database. 1 is the database owner.
uid is smallint.
This is the example to get the list of the objects, owned by the
database owner in the pubs database.
USE pubsGO
EXEC sp_MScheck_uid_owns_anything 1
GO
sp_columns_rowset
This stored procedure returns the complete columns description,
including the length, type, name, and so on.
Syntax
sp_columns_rowset table_name [, table_schema ] [, column_name]
where
- table_name
- is the table name. table_name is sysname.
- table_schema
- is the table schema. table_schema is sysname,
with a default of NULL.
- column_name
- is the column name. column_name is sysname,
with a default of NULL.
This is the example:
USE pubsGO
EXEC sp_columns_rowset 'authors'
GO
sp_fixindex
This stored procedure can be used to fix a corruption in a system table
by recreate the index.
Syntax
sp_fixindex dbname, tabname, indid
where
- dbname
- is the database name. dbname is sysname.
- tabname
- is the system table name. tabname is sysname.
- indid
- is the index id value. indid is int
Note. Before using this stored procedure the database has to be
in single user mode.
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:
USE pubsGO
EXEC sp_fixindex pubs, sysindexes, 2
GO
sp_MSforeachdb
Sometimes, you need to perform the same actions for all databases.
You can make cursor for this purpose, but you can also use
sp_MSforeachdb stored procedure in this case.
You can use this stored procedure to check all databases with
DBCC CHECKDB statement:
EXEC sp_MSforeachdb @command1="print '?' DBCC CHECKDB ('?')"
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_MShelpcolumns
This stored procedure returns the complete schema for a table, including
the length, type, name, and whether a column is computed.
Syntax
sp_MShelpcolumns tablename [, flags] [, orderby] [, flags2]
where
- tablename
- is the table name. tablename is nvarchar(517).
- flags
- flags is int, with a default of 0.
- orderby
- orderby is nvarchar(10), with a default of NULL.
- flags
- flags2 is int, with a default of 0.
To get the full columns description for the authors table in the
pubs database, run:
USE pubsGO
EXEC sp_MShelpcolumns 'authors'
GO
sp_MShelpindex
This stored procedure returns information about name, status, fill
factor, index columns names, and about used file group for the given
table.
Syntax
sp_MShelpindex tablename [, indexname] [, flags]
where
- tablename
- is the table name. tablename is nvarchar(517).
- indexname
- is the index name. indexname is nvarchar(258),
with a default of NULL.
- flags
- flags is int, with a default of NULL.
To get the indexes description for the authors table in the
pubs database, run:
USE pubsGO
EXEC sp_MShelpindex 'authors'
GO
sp_MShelptype
This stored procedure returns many useful information about system
data types and about user data types.
Syntax
sp_MShelptype [typename] [, flags]
where
- typename
- is the type name. typename is nvarchar(517),
with a default of NULL.
- flags
- flags is nvarchar(10), with a default of NULL.
To get information about all built-in and user defined data types
in the pubs database, run:
USE pubsGO
EXEC sp_MShelptype
GO
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
- is the table name. tablename is nvarchar(517).
- index_name
- is the index name. index_name is nvarchar(258),
with a default of NULL.
To determine the space used by the indexes from the authors table
in the pubs database, run:
USE pubsGO
EXEC sp_MSindexspace 'authors'
GO
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.
Syntax
sp_MSkilldb dbname
where
- dbname
- is the database name. dbname is nvarchar(258).
To kill the pubs database, run:
USE masterGO
EXEC sp_MSkilldb 'pubs'
GO
sp_MStablespace
This stored procedure returns the number of rows and the space
the table and index use.
Syntax
sp_MStablespace name [, id]
where
- name
- is the table name. name is nvarchar(517).
- id
- id is int, with a default of NULL.
To determine the space used by the authors table in the pubs database,
run:
USE pubsGO
EXEC sp_MStablespace 'authors'
GO
Here is the result set from my machine:
Rows DataSpaceUsed IndexSpaceUsed----------- ------------- --------------
23 8 32
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.
Syntax
sp_tempdbspace
This is the example:
EXEC sp_tempdbspace
Here is the result set from my machine:
database_name database_size spaceused------------- ----------------------- -----------------------------
tempdb 9.750000 .562500
sp_who2
This stored procedure returns information about current SQL Server 2000
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 [loginame]
where
- loginame
- the user's login name. If not specified, the procedure
reports on all active users of SQL Server.
This example returns information for the 'sa' login:
EXEC sp_who2 'sa'