En este caso, lo que puedes hacer es ver el código del procedimiento almacenado sp_spaceused
y encontrar el SELECT
que necesitas.
Lo revisé y modifiqué un poco para que lo puedas usar:
USE Northwind;
WITH CTE1 AS
(
SELECT SUM(CONVERT(bigint,CASE WHEN status & 64 = 0 THEN size ELSE 0 END)) dbsize,
SUM(CONVERT(bigint,CASE WHEN status & 64 <> 0 THEN size ELSE 0 END)) logsize
FROM dbo.sysfiles
), CTE2 AS
(
SELECT SUM(a.total_pages) reservedpages,
SUM(a.used_pages) usedpages,
SUM(
CASE
-- XML-Index and FT-Index internal tables are not considered "data", but is part of "index_size"
WHEN it.internal_type IN (202,204,211,212,213,214,215,216) THEN 0
WHEN a.type <> 1 THEN a.used_pages
WHEN p.index_id < 2 THEN a.data_pages
ELSE 0
END
) pages
FROM sys.partitions p
INNER JOIN sys.allocation_units a
ON p.partition_id = a.container_id
LEFT JOIN sys.internal_tables it
ON p.object_id = it.object_id
)
SELECT db_name() database_name,
LTRIM(STR((CONVERT(decimal(15,2),dbsize) + CONVERT(decimal(15,2),logsize))
* 8192 / 1048576,15,2) + ' MB'),
LTRIM(STR((CASE WHEN dbsize >= reservedpages THEN
(CONVERT(decimal(15,2),dbsize) - CONVERT(decimal(15,2),reservedpages))
* 8192 / 1048576 ELSE 0 END),15,2) + ' MB') [unallocated space]
FROM CTE1
CROSS JOIN CTE2;
De esta forma, puedes crear la tabla temporal e insertar los datos necesarios:
Use Northwind;
WITH CTE1 AS
(
SELECT SUM(CONVERT(bigint,CASE WHEN status & 64 = 0 THEN size ELSE 0 END)) dbsize,
SUM(CONVERT(bigint,CASE WHEN status & 64 <> 0 THEN size ELSE 0 END)) logsize
FROM dbo.sysfiles
), CTE2 AS
(
SELECT SUM(a.total_pages) reservedpages,
SUM(a.used_pages) usedpages,
SUM(
CASE
-- XML-Index and FT-Index internal tables are not considered "data", but is part of "index_size"
WHEN it.internal_type IN (202,204,211,212,213,214,215,216) THEN 0
WHEN a.type <> 1 THEN a.used_pages
WHEN p.index_id < 2 THEN a.data_pages
ELSE 0
END
) pages
FROM sys.partitions p
INNER JOIN sys.allocation_units a
ON p.partition_id = a.container_id
LEFT JOIN sys.internal_tables it
ON p.object_id = it.object_id
)
SELECT db_name() database_name,
LTRIM(STR((CONVERT(decimal(15,2),dbsize) + CONVERT(decimal(15,2),logsize))
* 8192 / 1048576,15,2) + ' MB'),
LTRIM(STR((CASE WHEN dbsize >= reservedpages THEN
(CONVERT(decimal(15,2),dbsize) - CONVERT(decimal(15,2),reservedpages))
* 8192 / 1048576 ELSE 0 END),15,2) + ' MB') [unallocated space]
INTO #Resultados
FROM CTE1
CROSS JOIN CTE2;