Thursday, August 12, 2010

sp_msforeachdb,sp_msforeachtable, sp_spaceused

sp_msforeachdb
This SP can be used to execute same T-SQL statement for all the databases on a server without specifying the Database naem explicitely.

For example if I want to perform Shrink operation on all teh databases ona server once a month, I can specify following query in a SQL Job

EXEC sp_MSForEachDB 'DBCC SHRINKDATABASE (''?'' , 0)'

sp_msforeachtable

Siliarly, If you want to perform same operation on all the tables ina database sp_msforeach table is averygood option. For exaple, if I want to build the Indexes of each table in a databse, I can use it in following way:

GO
EXEC sp_MSForEachTable 'ALTER INDEX ALL ON ? REBUILD '
GO


Ref: http://weblogs.sqlteam.com/joew/archive/2008/08/27/60700.aspx

sp_spaceused

this is a a wonderful SP which returns the disk space used by a table/Indexed View.

No comments:

Post a Comment