sql-servert-sql

Does sp_spaceused always return table sizes in KB?


According to Microsoft's documentation, the sp_spaceused stored procedure in SQL Server returns results in different units depending on the parameters provided.

For instance, when retrieving the size of the database, the result is in MB:

Size of the current database in megabytes.

sp_spaceused -- Calling without a parameter will return database_size in MB

However, for results related to tables, no specific unit is mentioned in the documentation:

Total amount of space used by data.

When passing a table name as a parameter, the results seem to be in KB, but it's not explicitly documented.

sp_spaceused 'MyTable' -- Results appear to be in KB

Is there any confirmation that the table sizes are always reported in KB, or does it depend on certain conditions?


Solution

  • Looking at the actual code (SQL Server 2019) in sp_SpaceUsed it is always reported in KB

    BEGIN
        SELECT
            OBJECT_NAME (@id) AS name,
            CONVERT (char(20), SUM(rows)) AS rows,
            LTRIM (STR (SUM (CAST (SUBSTRING(reserved, 1, CHARINDEX(' ', reserved)) AS bigint)),15,0) + ' KB' ) AS reserved,
            LTRIM (STR (SUM (CAST (SUBSTRING(data, 1, CHARINDEX(' ', data)) AS bigint)),15,0) + ' KB') AS data,
            LTRIM (STR (SUM (CAST (SUBSTRING(index_size, 1, CHARINDEX(' ', index_size)) AS bigint)),15,0) + ' KB') AS index_size,
            LTRIM (STR (SUM (CAST (SUBSTRING(unused, 1, CHARINDEX(' ', unused)) AS bigint)),15,0) + ' KB') AS unused
        FROM 
            @detail_tmp_table
    END