-
Notifications
You must be signed in to change notification settings - Fork 0
/
List-TablesRows.SQL
25 lines (24 loc) · 1.06 KB
/
List-TablesRows.SQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--list tables in database, sort by schema and table name with comments and number of rows in each table.
-- copied from https://dataedo.com/blog/useful-sql-server-data-dictionary-queries-every-dba-should-have
select schema_name(tab.schema_id) as schema_name,
tab.name as table_name,
tab.create_date as created,
tab.modify_date as last_modified,
p.rows as num_rows,
ep.value as comments
from sys.tables tab
inner join (select distinct
p.object_id,
sum(p.rows) rows
from sys.tables t
inner join sys.partitions p
on p.object_id = t.object_id
group by p.object_id,
p.index_id) p
on p.object_id = tab.object_id
left join sys.extended_properties ep
on tab.object_id = ep.major_id
and ep.name = 'MS_Description'
and ep.minor_id = 0
and ep.class_desc = 'OBJECT_OR_COLUMN'
order by num_rows DESC