|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Introduction
Using the Stored Proceduressp_msforeachdbExample #1 - to do a check db on every database in your instance you could issue the following command: sp_msforeachdb 'dbcc checkdb( ''?'' )'
Example #2 - to change the owner of each database in the instance to sa. sp_msforeachdb 'IF ''?'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'')
BEGIN
print ''?''
exec [?].dbo.sp_changedbowner ''sa''
END'
**Notice how I used an Example #3 - to do a check db on every table in the database you could issue the following command: sp_msforeachdb 'dbcc checktable( ''?'' )'
Example #4 - to shrink every database on the instance. Be careful with this one. Not something you want to run on a production server during business hours. sp_msforeachdb 'dbcc ShrinkDatabase( ?, 10 )'
Example #5 - to make a user db_owner on each user database in the instance. This is commonly done for apps like SharePoint that require db_owner in order to apply service packs. sp_msforeachdb 'IF ''?'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'')
BEGIN
print ''?''
exec [?].dbo.sp_adduser ''<YOUR DOMAIN NAME HERE>\<YOUR USER ACCOUNT HERE>''
exec [?].dbo.sp_addrolemember ''db_owner'',''<YOUR DOMAIN NAME HERE>\
<YOUR USER ACCOUNT HERE''
END'
sp_msforeachtableThe counterpart to Example #1 - to get a list of each index and when the statistics were last updated on each index. CREATE table #stats(
table_name nvarchar(255) null,
index_name nvarchar(255) null,
statistics_update_date datetime null
)
GO
exec sp_msforeachtable
'insert into #stats
SELECT
''?'',
name AS index_name,
STATS_DATE(object_id, index_id) AS statistics_update_date
FROM
sys.indexes
WHERE
object_id = OBJECT_ID(''?'');'
select * from #stats where index_name is not null
drop table #stats
There are a million different uses for these stored procedures. The possibilities are endless. You can even nest a
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||