Blog Pages

sp_MSforeachtable

sp_MSforeachtable perform an action on all of the tables within a database.

exec sp_MSforeachtable @command1, @replacechar, @command2, @command3, @whereand, @precommand, @postcommand
  • @command1 - the command to be executed by sp_MSforeachtable.
  • @replacechar - a character in the command string that will be replaced with the table name being processed (default '?').
  • @command2 and @command3 are two additional optional commands that can be run for each table (@command2 will be run after @command1, @command3 will be run after @command2).
  • @whereand - an additional constraints to help identify the rows in the sysobjects table that will be selected.
  • @precommand - a command to be run prior to processing any table
  • @postcommand - a command to be run after all commands have been processed against all tables

Examples:
declare @cmd1 varchar(500)
set @cmd1 = 'exec sp_Help "?"'
exec sp_MSforeachtable @command1=@cmd1
GO


EXEC sp_MSforeachtable @command1="print '*'; DBCC DBREINDEX ('*', ' ', 80)" ,@replacechar='*'
GO

No comments:

Post a Comment