Skip to main content

Usage syntax for the SQL Server undocumented Stored Procedures sp_msforeachtable and sp_msforeachdb. Performs operations on each database or table.

-- =================
-- sp_msforeachtable
-- =================

-- The "sp_msforeachtable" SP comes with SQL Server, but it is not documented in
-- Books Online. This SP can be found in the "master" database and is used to
-- process a single T-SQL command or a number of different T-SQL commands against
-- every table in a given database.

exec @return_value = sp_msforeachtable
    @command1,
    @replacechar,
    @command2,
    @command3,
    @whereand,
    @precommand,
    @postcommand;

-- -----------------------------------------------------------------------------
-- @return_value - is the return value which will be set by "sp_msforeachtable".
-- @command1     - is the first command to be executed by "sp_msforeachtable"
--                 and is defined as a nvarchar(2000).
-- @replacechar  - is a character in the command string that will be replaced
--                 with the table name being processed (default replacechar is a
--                 "?").
-- @command2     - (and @command3) are two additional commands that can be run
--                 for each table, where @command2 runs after @command1, and
--                 @command3 will be run after @command2.
-- @whereand     - this parameter can be used to add additional constraints to
--                 help identify the rows in the sysobjects table that will be
--                 selected, this parameter is also a nvarchar(2000).
-- @precommand   - is a nvarchar(2000) parameter that specifies a command to be
--                 run prior to processing any table.
-- @postcommand  - is also a nvarchar(2000) field used to identify a command to
--                 be run after all commands have been processed against all
--                 tables.
-- -----------------------------------------------------------------------------

-- ==============
-- sp_msforeachdb
-- ==============

-- The SP "sp_msforeachdb" is found in the "master" database. This SP is used to
-- execute a single T-SQL statement, like "DBCC CHECKDB" or a number of T-SQL
-- statements against every database defined to a SQL Server instance. Here is the
-- syntax for calling this undocumented SP:

exec @return_value = sp_msforeachdb
    @command1,
    @replacechar,
    @command2,
    @command3,
    @precommand,
    @postcommand;

-- -----------------------------------------------------------------------------
-- @return_value - is the return value which will be set by "sp_msforeachdb".
-- @command1     - is the first command to be executed by "sp_msforeachdb" and
--                 is defined as nvarchar(2000).
-- @replacechar  - is a character in the command string that will be replaced
--                 with the table name being processed (default replacechar is a
--                 "?").
-- @command2     - (and @command3) are two additional commands that can be run
--                 against each database.
-- @precommand   - is a nvarchar(2000) parameter that specifies a command to be
--                 run prior to processing any database.
-- @postcommand  - is also an nvarchar(2000) field used to identify a command to
--                 be run after all commands have been processed against all
--                 databases.
-- -----------------------------------------------------------------------------