Hi,@mo boy
Try the following code:
alter PROC dbo.sp_DBA_spaceused_AllTables2
AS
SET NOCOUNT ON
SELECT
DB_NAME() AS DatabaseNase ,
GETDATE() AS DateCreated ,
TableName,
Rows ,
Reserved ,
Data ,
Used - Data AS IndexSize ,
Reserved - Used AS Unused
FROM (
SELECT
USR.name + '.' + OBJ.name AS TableName ,
MIN(row_count) AS Rows ,
SUM(8 * reserved_page_count) + MAX(COALESCE(LOBDATA.LobReserved,0)) AS reserved,
SUM (8*
CASE
WHEN (index_id < 2) THEN (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)
ELSE lob_used_page_count + row_overflow_used_page_count
END
) AS Data,
SUM (8*used_page_count)+MAX(COALESCE(LOBDATA.LobUsed,0)) AS Used
FROM dbo.sysobjects AS OBJ
INNER JOIN sys.schemas AS USR
ON OBJ.uid = USR.schema_id
INNER JOIN sys.dm_db_partition_stats AS PS
ON PS.object_id = OBJ.id
LEFT JOIN(
SELECT
parent_id,
SUM(8*reserved_page_count) AS LOBReserved,
SUM(8*used_page_count) AS LOBUsed
FROM sys.dm_db_partition_stats p
INNER JOIN sys.internal_tables it
ON p.object_id = it.object_id
WHERE it.internal_type IN (202,204)
GROUP BY IT.parent_id
) AS LOBDATA
ON LOBDATA.parent_id = OBJ.Id
WHERE OBJ.type='U'
GROUP BY USR.name + '.' + OBJ.name
) AS DT
return;
exec master.dbo.sp_msforeachdb @command1="exec [?].dbo.sp_DBA_spaceused_AllTables2"
CREATE TABLE ##SpaceUsedResults (
DatabaseName SYSNAME NOT NULL,
DateCreated DATETIME NOT NULL,
TableName NVARCHAR(256) NOT NULL,
Rows BIGINT NOT NULL,
Reserved BIGINT NOT NULL,
Data BIGINT NOT NULL,
IndexSize BIGINT NOT NULL,
Unused BIGINT NOT NULL,
CONSTRAINT PK_SpaceUsed PRIMARY KEY CLUSTERED (DatabaseName,TableName,DateCreated)
)
exec master.dbo.sp_msforeachdb @command1="INSERT INTO ##SpaceUsedResults exec [?].dbo.sp_DBA_spaceused_AllTables2"
SELECT * FROM ##SpaceUsedResults
Best regards,
Bert Zhou
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.