Search This Blog

Sunday, August 28, 2011

6 Common Uses of the undocumented Stored Procedure sp_MSforeachdb

Print all the database names in a SQL Server Instance
EXEC sp_MSforeachdb 'USE ?; PRINT DB_NAME()'

Print all the tables in all the databases of a SQL Server Instance
EXEC sp_MSforeachdb 'USE ? SELECT DB_NAME() + ''.'' + OBJECT_NAME(object_Id) FROM sys.tables'

EXEC sp_MSforeachdb 'USE ? SELECT OBJECT_NAME(object_Id) FROM sys.tables where DB_NAME() NOT IN(''master'', ''model'', ''msdb'', ''tempdb'')'

Display the size of all databases in a SQL Server instance
EXEC sp_MSforeachdb 'USE ?; EXEC sp_spaceused'

Determine all the physical names and attributes(size,growth,usage) of all databases in a SQL Server instance
EXEC sp_MSforeachdb 'USE ? EXEC sp_helpfile;'

Change Owner of all databases to 'sa'
EXEC sp_MSforeachdb 'USE ?; EXEC sp_changedbowner ''sa'''

Check the Logical and Physical integrity of all objects in the database
sp_MSforeachdb 'DBCC CHECKDB(?)'

















 

No comments:

Post a Comment