Sometimes you may want to run a piece of code for every table. This can be accomplished using the function sp_MSforeachtable. For example

1
EXEC sp_MSforeachtable 'SELECT ''?'' [Table], Count(*) [Count] FROM ?';

Will count the number of records in each table

1
EXEC sp_MSforeachtable 'TRUNCATE TABLE ?';

Will purge each table

1
EXEC sp_MSforeachtable 'ALTER TABLE ? REBUILD';

Will rebuild the indexes

And so on..