Tuesday, 21 April 2020

How do I get all table count in all databases from the SQL Server Instance by using T-SQL Script ?



Declare @sql nvarchar(max);
Select @sql =
    (select ' UNION ALL
        select @@ServerName As ServerName ,' +  + quotename(name,'''') + ' as database_name,                              Count( t.name)  as table_name
       FROM '+ quotename(name) + '.sys.tables t
         JOIN '+ quotename(name) + '.sys.schemas s   on s.schema_id = t.schema_id'
    from sys.databases
    where state=0 and database_id>4
--Group by name
    order by [name] for xml path(''), type).value('.', 'nvarchar(max)');
set @sql = stuff(@sql, 1, 12, '') + ' order by database_name,table_name';
execute (@sql)

No comments:

Post a Comment