A Couple Of T-SQL Quickies

It’s been a while since I’ve posted any SQL Server content, so in a bid to get back into the swing of things, here are a couple of very simple, but useful, T-SQL commands. I’ve used these recently in a large project I’m working on.

Identify all tables with no Foreign Key constraints:

select distinct
t.name
from sys.tables t
left outer join sys.foreign_keys fk on t.object_id = fk.parent_object_id
where fk.object_id is null
order by t.name;

List all tables in a database, with row counts

select t.name ,
ps.row_count
from sys.dm_db_partition_stats ps
inner join sys.tables t on ps.object_id = t.object_id
where ps.index_id < 2
order by t.name;

Leave a Reply