Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Helpful Sql Server scripts
-- See lots of settings the DB is currently using
select * from sys.databases
-- Count rows in each table
select TableName, i.rows Records
from sysobjects t, sysindexes i
where t.xtype = 'U' and = and i.indid in (0,1)
order by Records desc;
-- Find Foreign Key References
-- From
select cast( as varchar(255)) as foreign_key_name
, r.keycnt
, cast( as varchar(255)) as foreign_table
, cast( as varchar(255)) as foreign_column_1
, cast( as varchar(255)) as foreign_column_2
, cast( as varchar(255)) as primary_table
, cast( as varchar(255)) as primary_column_1
, cast( as varchar(255)) as primary_column_2
from sysobjects f
inner join sysobjects c on f.parent_obj =
inner join sysreferences r on = r.constid
inner join sysobjects p on r.rkeyid =
inner join syscolumns rc on r.rkeyid = and r.rkey1 = rc.colid
inner join syscolumns fc on r.fkeyid = and r.fkey1 = fc.colid
left join syscolumns rc2 on r.rkeyid = and r.rkey2 = rc.colid
left join syscolumns fc2 on r.fkeyid = and r.fkey2 = fc.colid
where f.type = 'F'
ORDER BY cast( as varchar(255))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.