SQL get table row count for specific table in DB

How Can i iterate thru the all my DB's and get a row count for each employee table? Each client is has there own DB, need to find the total employees in each DB.

Been trying to figure out how to use sp_MSforeachdb

sp_MSforeachdb 
@command1 = 'select count(*) from employee'

Can output in seperate tables or would be good in one table wiht the DB name.


How about

    DECLARE @sql nvarchar(1000)
    SET @sql = 'Use [?];'
      + 'IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ''dbo'' AND  TABLE_NAME = ''employee''))'
      + ' BEGIN'
      + ' SELECT COUNT(*) from [employee]'
      + ' END'
   exec sp_MSforeachDB @sql

TABLE_SCHEMA = ''dbo'' is optional here in most cases...


您需要首先告诉它要使用哪个数据库(在? ):

EXEC sp_MSforeachdb
@command1='use ?; select count(*) from employee'  
链接地址: http://www.djcxy.com/p/94458.html

上一篇: 在另一个操作后执行Redux调度操作

下一篇: SQL获取数据库中特定表的表行数