Select SQL Server database size

前端 未结 10 890
遇见更好的自我
遇见更好的自我 2020-11-30 16:47

how can i query my sql server to only get the size of database?

I used this :

use \"MY_DB\"
exec sp_spaceused

I got this :

<
相关标签:
10条回答
  • 2020-11-30 17:18

    Worked perfectly for me to calculate SQL database size in SQL Server 2012

    exec sp_spaceused
    

    0 讨论(0)
  • 2020-11-30 17:18

    If you want to simply check single database size, you can do it using SSMS Gui

    Go to Server Explorer -> Expand it -> Right click on Database -> Choose Properties -> In popup window choose General tab ->See Size

    Source: Check database size in Sql server ( Various Ways explained)

    0 讨论(0)
  • 2020-11-30 17:20

    EXEC sp_spaceused @oneresultset = 1 show in 1 row all of the result

    if you execute just 'EXEC sp_spaceused' you will see two rows Work in SQL Server Management Studio v17.9

    0 讨论(0)
  • 2020-11-30 17:20

    Check Database Size in SQL Server for both Azure and On-Premises-

    Method 1 – Using ‘sys.database_files’ System View

    SELECT
        DB_NAME() AS [database_name],
        CONCAT(CAST(SUM(
            CAST( (size * 8.0/1024) AS DECIMAL(15,2) )
        ) AS VARCHAR(20)),' MB') AS [database_size]
    FROM sys.database_files;
    

    Method 2 – Using ‘sp_spaceused’ System Stored Procedure

    EXEC sp_spaceused ;
    

    0 讨论(0)
  • 2020-11-30 17:21

    You can check how this query works following this link.

        IF OBJECT_ID('tempdb..#spacetable') IS NOT NULL 
        DROP TABLE tempdb..#spacetable 
        create table #spacetable
        (
        database_name varchar(50) ,
        total_size_data int,
        space_util_data int,
        space_data_left int,
        percent_fill_data float,
        total_size_data_log int,
        space_util_log int,
        space_log_left int,
        percent_fill_log char(50),
        [total db size] int,
        [total size used] int,
        [total size left] int
        )
        insert into  #spacetable
        EXECUTE master.sys.sp_MSforeachdb 'USE [?];
        select x.[DATABASE NAME],x.[total size data],x.[space util],x.[total size data]-x.[space util] [space left data],
        x.[percent fill],y.[total size log],y.[space util],
        y.[total size log]-y.[space util] [space left log],y.[percent fill],
        y.[total size log]+x.[total size data] ''total db size''
        ,x.[space util]+y.[space util] ''total size used'',
        (y.[total size log]+x.[total size data])-(y.[space util]+x.[space util]) ''total size left''
         from (select DB_NAME() ''DATABASE NAME'',
        sum(size*8/1024) ''total size data'',sum(FILEPROPERTY(name,''SpaceUsed'')*8/1024) ''space util''
        ,case when sum(size*8/1024)=0 then ''divide by zero'' else
        substring(cast((sum(FILEPROPERTY(name,''SpaceUsed''))*1.0*100/sum(size)) as CHAR(50)),1,6) end ''percent fill''
        from sys.master_files where database_id=DB_ID(DB_NAME())  and  type=0
        group by type_desc  ) as x ,
        (select 
        sum(size*8/1024) ''total size log'',sum(FILEPROPERTY(name,''SpaceUsed'')*8/1024) ''space util''
        ,case when sum(size*8/1024)=0 then ''divide by zero'' else
        substring(cast((sum(FILEPROPERTY(name,''SpaceUsed''))*1.0*100/sum(size)) as CHAR(50)),1,6) end ''percent fill''
        from sys.master_files where database_id=DB_ID(DB_NAME())  and  type=1
        group by type_desc  )y'
        select * from #spacetable 
        order by database_name
        drop table #spacetable
    
    0 讨论(0)
  • 2020-11-30 17:22

    Also compare the results with the following query's result

    EXEC sp_helpdb @dbname= 'MSDB'
    

    It produces result similar to the following

    enter image description here

    There is a good article - Different ways to determine free space for SQL Server databases and database files

    0 讨论(0)
提交回复
热议问题