SQL SERVER – Query to find number Rows, Columns, ByteSize for each table in the current database – Find Biggest Table in Database

This query shows rows, columns and size for each table, so you can spot the biggest table in database.

SQL SERVER - Query to find number Rows, Columns, ByteSize for each table in the current database - Find Biggest Table in Database


USE DatabaseName
GO
CREATE TABLE #temp (
table_name sysname ,
row_count INT,
reserved_size VARCHAR(50),
data_size VARCHAR(50),
index_size VARCHAR(50),
unused_size VARCHAR(50))
SET NOCOUNT ON
INSERT #temp
EXEC sp_msforeachtable 'sp_spaceused ''?'''
SELECT a.table_name,
a.row_count,
COUNT(*) AS col_count,
a.data_size
FROM #temp a
INNER JOIN information_schema.columns b
ON a.table_name collate database_default
= b.table_name collate database_default
GROUP BY a.table_name, a.row_count, a.data_size
ORDER BY CAST(REPLACE(a.data_size, ' KB', '') AS integer) DESC
DROP TABLE #temp

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL Scripts, SQL Stored Procedure
Previous Post
SQL SERVER – Simple Example of Cursor
Next Post
SQL SERVER – Query Analyzer Shortcuts

Related Posts

276 Comments. Leave new

  • can anybody solve this query, i have an employee table and i need to show all the data of those Employees who are Emp_Status ‘P’ (PERMANENT) and’B’ (PROBATIONARY)

    Reply
  • Hi Pinal

    i want to shoot a mail once in a day only through asp.net and

    sql can you please tell me whats the query for this

    waiting to your reply

    Dev

    Reply
  • Hi,

    Why don’t you configure mail in SQL SERVER and develop a Stored Procedure which will be schedule to run at once a day.

    This will reduce effort to maintain your task.

    To configure mail in SQL, please refer:

    Thanks,

    Tejas

    Reply
  • Hi i dont know how can configure mail server in sql server
    can u please tell me

    Dev1

    Reply
  • i am using sql server 2000

    Reply
  • Great job, works like a charm.

    Reply
  • Hello, -How i can search the special characters (char(10),char(13),…) in every table and field in a SQL Server Database ?

    Thanks, S.P.

    Reply
  • hi pinal,

    I have 1 table which contain DateTime, Enployee Number and usetype (used for IN or OUT) column, this is for time and attendance. Now I want to know how many employees are inside the campus and their employee ids. I am trying it with max(intime)>max(outtime) but i m not getting the desired result.
    sample data-

    emp no occurdatetime usetype (1 for IN,2
    for OUT)
    2099 2009-01-28 11:25:21.000 1
    3652 2009-01-28 11:26:12.000 1
    3607 2009-01-28 11:26:13.000 1
    6270 2009-01-28 11:26:39.000 2
    4489 2009-01-28 11:26:53.000 1
    6536 2009-01-28 11:27:00.000 1
    1742 2009-01-28 11:27:02.000 2
    2579 2009-01-28 11:27:09.000 1
    3853 2009-01-28 11:28:00.000 2
    5762 2009-01-28 11:29:09.000 1
    5668 2009-01-28 11:29:31.000 2
    4956 2009-01-28 11:31:43.000 2

    and how to convert this datetime data formate to Date and time separately.

    Please give me some clue to move further.

    thanks

    Reply
  • Thank you so much! great help

    Reply
  • Thank you for this one…

    Reply
  • Thanks you. Great query. Works without any trouble.

    Reply
  • How do u calculating the columns using above query………

    Reply
  • i am working on SQL. i want to set a counter for primary key which has some data input, so later when i add other rows the primary kry should increase automatically.. Primary key is “Prot1”
    Nxt should be “Prot2”

    Reply
    • Two options

      1 Use identity column and when you select it prefix prot with it
      2 Use identity column and a another computed column that has the definition of

      col as ‘Prot’+cast(idcol as varchar(10))

      Reply
  • Atiq Ur Rahman Chaudhary
    July 4, 2010 2:09 am

    Great, very useful blog.

    Reply
  • Hi there!,

    Here you can find the sql command to get number of COLUMNS in a table….

    select TABLE_NAME, count(*) COLUMNS1 from all_tab_columns where owner=’EMP’
    GROUP BY TABLE_NAME
    ORDER BY COLUMNS1
    /

    Reply
  • Hi,

    I have a requirement like, I want to know the number of columns in a temp table.

    Thanks in advance
    Madan

    Reply
    • Hi,

      I used the below queries to fetch the number of columns in a temp table.

      select count(*) from information_schema.columns
      where table_name = ‘#temptable’

      select count(*) Noofcolumns from SYSCOLUMNS
      where id=(select id from SYSOBJECTS where name=’#temptable’)

      both the above queries didn’t give the desired result.

      Please tell me how to retrieve the number of columns in a temp table.

      Thanks in advance,
      Madan

      Reply
  • How to display count and names of Databases in SQL Server

    Reply
  • Im having some errors here:

    ON a.table_name collate database_default
    = b.table_name collate database_default
    GROUP BY a.table_name, a.row_count, a.data_size
    ORDER BY CAST(REPLACE(a.data_size, ‘ KB’, ”) AS integer) DESC
    DROP TABLE #temp

    What names should i use? i dont understand this part…

    Thanks for reading!

    Reply
  • I have two table a where the coulmn is date and userid and another table emp_info contain userid,name dept now I want to find the information from emp_info who is absent in table a between two date where I may assign the holiday.

    Please help me

    Reply
  • lots of thanks for ur work.. i got my problem solved in short time for finding numbers of rows of each table.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.