Wednesday, September 19, 2007

Count Rows

select rowcnt from sysindexes where id = object_id('tablename') and indid = 1 --assuming there exist a clustered index

select rowcnt from sysindexes where id = object_id('tablename') and indid = 0 --assuming table is a heap

Run following to fix errors with non-clustered indexes:
dbcc updateusage(0,'t') with count_rows

n-th largest/smallest value in a table

SELECT *
FROM Employee E1
WHERE (N-1) = (SELECT COUNT(DISTINCT(E2.Salary))
FROM Employee E2
WHERE E2.Salary > E1.Salary )


Change > TO < for Smallest Number

B-tree reference

http://en.wikipedia.org/wiki/B-tree

SQL Memory

SQL Server memory is primarily used to store data (buffer cache) and query plans (procedure cache).