Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql server size of every table in a db

SELECT 
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
    CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    TotalSpaceMB DESC, t.Name
Comment

PREVIOUS NEXT
Code Example
Sql :: sqlplus change user 
Sql :: datetrunc month sql 
Sql :: How to take sum of column with same id and different table in SQL? 
Sql :: oracle insert from select 
Sql :: create table kusto 
Sql :: Question 7: Write an SQL query to print details of the Workers who have joined in Feb’2014. 
Sql :: excel vba import data to sql server 
Sql :: mysql average from two table 
Sql :: offset in postgresql example 
Sql :: Failed to process SQL command - ORA-28014: cannot drop administrative user or role 
Sql :: pl sql if boolean 
Sql :: delete from table where length sql 
Sql :: linux upload database to mysql 
Sql :: mysql dump structure only 
Sql :: drop tables from local database postgres pgadmin 
Sql :: sql average 
Sql :: into in sql 
Sql :: systems sql 
Sql :: creating a database sql 
Sql :: Ms Sql set us timezone 
Sql :: get last inserted primary key 
Sql :: table users 
Sql :: sql query to delete duplicate records 
Sql :: sql join 3 tables 
Sql :: sql update subtract value 
Sql :: json object to column value in sql server 
Sql :: .sql File Run in PSQL 
Sql :: postgres insert 
Sql :: indexing in mysql 
Sql :: sql query examples 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =