Joe Webb

Musing and observations about SQL Server, other technogies, and sometimes just life in general
posts - 85, comments - 401, trackbacks - 0

My Links

News

This blog has moved!

Click here for the new location.

Follow me on Twitter
Add to Technorati Favorites

Search this Blog
 




Archives

Post Categories

About me

The undocumented sp_MSforeachtable procedure

As database administrators or developers, sometimes we need to perform an action on all of the tables within a database. For example, we may want to disable check constraints for all tables prior to loading a bunch of data; or perhaps we need to run DBCC CHECKTABLE for every table in our database to ensure the integrity of all pages and structures that make up the tables.

We could, of course, create a script that uses a cursor to loop through all of the tables in the database to do our bidding through dynamic SQL. Yuck!

Fortunately, there's a better way. It's the undocumented sp_MSforeachtable stored procedure in the master database. It's like a cursor in that it loops through each table in the current database and executes a script that you define. But it requires considerably less code.

For example, the following script checks the integrity of each table in the AdventureWorks database using the DBCC CHECKTABLE command. Notice that a [?] is used as a placeholder for the table name in the SQL statement.

USE AdventureWorks;
EXECUTE sp_MSforeachtable 'DBCC CHECKTABLE ([?])';

That's a whole lot more palatable than developing a cursor-based solution!

Here's another example. The following script reports the space used and allocated for every table in the database.

USE AdventureWorks;
EXECUTE sp_MSforeachtable 'EXECUTE sp_spaceused [?];';

So, the next time you need to loop through each table, give the sp_MSforeachtable procedure a try.

Cheers!

Joe

kick it on DotNetKicks.com

Print | posted on Tuesday, October 23, 2007 11:21 AM | Filed Under [ T-SQL SQL Server ]

Feedback

Gravatar

# re: The undocumented sp_MSforeachtable procedure

Thanks, needed this for my advanced database class!
10/6/2009 9:51 PM | Fanshawe Student.
Gravatar

# re: The undocumented sp_MSforeachtable procedure

Nice. I took this to its logical extreme: here's something to reindex every table in every database, then truncate the logs and shrink the database files; hope this proves useful to someone:

begin
declare @dbname varchar(50)
declare @logname varchar(50)
declare @sql varchar(2000)
declare cDB cursor for select name from sys.databases
open cDB
fetch next from cDB into @dbname
while @@fetch_status=0
begin
exec ('use ['+@dbname+']')
exec ('sp_MSForEachTable @command1="dbcc dbreindex (''?'')"')
begin try
exec ('alter database ['+@dbname+'] set recovery simple')
declare cLog cursor for select name from sys.database_files where type_desc='LOG'
open cLog
fetch next from cLog into @logname
while @@fetch_status=0
begin
begin try
dbcc shrinkfile (@logname,0)
end try begin catch end catch
fetch next from cLog into @logname
end
close cLog
deallocate cLog
dbcc shrinkdatabase (@dbname, TRUNCATEONLY)
exec ('alter database ['+@dbname+'] set recovery full')
end try begin catch end catch
fetch next from cDB into @dbname
end
close cDB
deallocate cDB
end

4/19/2010 1:33 PM | Dan Sutton

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 1 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET