|
|
| |
SQL Server 7.0 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 7.0.
sp_columns_rowset
This stored procedure returns the complete columns description,
including the length, type, name, and so on.
Syntax
sp_columns_rowset tbname [, table_schema ] [, column_name]
where
tbname - is the table name. tbname 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 pubs
GO
EXEC sp_columns_rowset 'authors'
GO
|
sp_fixindex
This stored procedure can be used to fix corruption in a system table
by recreating the index.
Syntax
sp_fixindex database, systemcatalog, ind_id
where
database - is the database name. database is sysname.
systemcatalog - is the system table name. systemcatalog is sysname.
ind_id - is the index id value. ind_id 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?
This is the example:
USE pubs
GO
EXEC sp_fixindex pubs, sysindexes, 2
GO
|
sp_MSexists_file
Used to determine whether a particular file exists in a particular
folder or not.
Syntax
sp_MSexists_file full_path, filename
where
full_path - is the full path to the file. full_path is nvarchar(512).
filename - is the file name. filename is nvarchar(255).
|
To check if file textcopy.exe exists in the C:\MSSQL7\BINN\ directory
(path by default), run:
DECLARE @retcode int
EXEC @retcode = sp_MSexists_file 'C:\MSSQL7\BINN\', 'textcopy.exe'
IF @retcode = 1
PRINT 'File Exist'
ELSE
PRINT 'File does not Exist'
|
sp_MSforeachdb
Sometimes, you need to perform the same actions for all databases.
You can create cursor for this purpose, or you can also use sp_MSforeachdb
stored procedure to accomplish the same goal with less work.
For example, you can use this stored procedure to run a CHECKDB for all
the databases on your server.
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 create cursor for this purpose, or you can also use
sp_MSforeachtable stored procedure to accomplish the same goal with less work.
For example, you can use this stored procedure to rebuild all the
indexes in a database.
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 pubs
GO
EXEC sp_MShelpcolumns 'authors'
GO
|
sp_MShelpindex
This stored procedure returns information about name, status, fill factor,
index columns names, and file groups for a 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).
flags - flags is int, with a default of NULL.
|
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 much useful information about system
data types and 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 pubs
GO
EXEC sp_MShelptype
GO
|
sp_MSindexspace
This stored procedure returns the size in kb, of the indexes found
in a particular table.
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 pubs
GO
EXEC sp_MSindexspace 'authors'
GO
|
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.
Syntax
sp_MSkilldb dbname
where
dbname - is the database name. dbname is nvarchar(258).
|
To kill the pubs database, run:
USE master
GO
EXEC sp_MSkilldb 'pubs'
GO
|
sp_MStablespace
This stored procedure returns the number of rows in a table 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 pubs
GO
EXEC sp_MStablespace 'authors'
GO
|
Here is the result set from my machine:
Rows DataSpaceUsed IndexSpaceUsed
----------- ------------- --------------
23 8 24
|
sp_tempdbspace
This stored procedure can be used to get the total size and the space
used by the tempdb database. It is used 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 8.500000 .703125
|
sp_who2
This stored procedure returns information about current SQL Server 7.0
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 data provided by 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'
|
|
|