SQL SERVERR中未公开的存储过程sp_MSforeachtable

   该存储过程用来遍历数据库中的每个表,常见用法如下:   
   1.  统计数据库中每个表的空间使用情况,返回各表的行数,数据大小,索引大小,未使用空间等。
      exec sp_MSforeachtable @command1="sp_spaceused '?'"
   2.-查询数据库所有表的记录总数
CREATE TABLE #temp (TableName VARCHAR (255), RowCnt INT)
EXEC sp_MSforeachtable 'INSERT INTO #temp SELECT ''?'', COUNT(*) FROM ?'
SELECT TableName, RowCnt FROM #temp ORDER BY TableName
DROP TABLE #temp
   3.删除所有表
   exec sp_MSforeachtable @command1="drop table ?";
   4.清除所有表中数据
   exec sp_MSforeachtable @command1="TRUNCATE TABLE ?"

posted @ 2010-09-13 23:33  John Liu  阅读(549)  评论(0编辑  收藏  举报