Tuesday, January 15, 2013

SQL- Query to find Rows count,Table Size along with Columns Count in database




USE DatabaseName     --Replace Database Name
GO

CREATE TABLE #temp (          --Creating New Temp Table
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                        --Insert Infomation into Temp Table
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              --Drop Temp Table



1 comment:

  1. Casino Finder (Google Play) Reviews & Demos - Go
    Check wooricasinos.info Casino deccasino Finder (Google Play). A look หารายได้เสริม at some of the best 출장마사지 gambling gri-go.com sites in the world. They offer a full game library,

    ReplyDelete