一尘不染

您可以根据SQL Server许可证查询最大数据库大小吗?

sql

根据您的SQL Server数据库的许可和配置,您可能会有非常不同的大小限制。例如:

  • SQL Server 2012 Express的限制为10GB
  • SQL Server 2008 R2 Express的限制为10GB
  • SQL Server 2008 Express的限制为4GB

是否可以 查询数据库服务器以找出最大大小

我可以查询以找出当前大小:

name      DataFileSizeMB LogFileSizeMB  
--------- -------------- -------------  
master          4.875000      1.750000  
tempdb          8.000000      0.500000  
model           4.062500      1.000000  
msdb           16.687500      4.562500  
Harrison       21.937500     18.187500  
Lennon        122.750000    425.625000  
McCartney      19.937500     49.687500  
Starr          19.937500     18.187500

我现在想要的是一种基于 许可 而不是可用HDD空间来找出最大数量的方法。


阅读 221

收藏
2021-03-08

共1个答案

一尘不染

试试这个-

SELECT 
      d.server_name
    , d.sversion_name
    , d.edition
    , max_db_size_in_gb = 
        CASE WHEN engine_edition = 4 
            THEN 
                CASE 
                    WHEN d.sversion_name LIKE '%2012%' THEN 10
                    WHEN d.sversion_name LIKE '%2008 R2%' THEN 10
                    WHEN d.sversion_name LIKE '%2008%' THEN 4 
                    WHEN d.sversion_name LIKE '%2005%' THEN 4
                END
            ELSE -1 
        END
FROM (
    SELECT 
          sversion_name = SUBSTRING(v.ver, 0, CHARINDEX('-', v.ver) - 1)
        , engine_edition = SERVERPROPERTY('EngineEdition')
        , edition = SERVERPROPERTY('Edition')
        , server_name = SERVERPROPERTY('ServerName')
    FROM (SELECT ver = @@VERSION) v
) d

另外,请尝试使用此查询来获取有关DB的扩展信息-

IF EXISTS(
    SELECT 1 
    FROM tempdb.dbo.sysobjects 
    WHERE id = OBJECT_ID('tempdb.dbo.#DBObj')
) DROP TABLE #DBObj

CREATE TABLE #DBObj
(
      [DB] SYSNAME
    , reservedpages INT
    , usedpages INT
    , pages INT
)

EXEC sys.sp_MSforeachdb '
    USE [?]

    INSERT INTO #DBObj
    (
          [DB]
        , reservedpages
        , usedpages
        , pages
    )
    SELECT 
          DB_NAME()
        , pg.reservedpages
        , pg.usedpages
        , pg.pages   
    FROM (
        SELECT
              reservedpages = SUM(a.total_pages)
            , usedpages = SUM(a.used_pages)
            , pages = SUM(
                CASE
                    WHEN it.internal_type IN (202, 204, 207, 211, 212, 213, 214, 215, 216, 221, 222) THEN 0
                    WHEN a.[type] != 1 AND p.index_id < 2 THEN a.used_pages
                    WHEN p.index_id < 2 THEN a.data_pages ELSE 0
                END
              )
        FROM sys.partitions p
        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]
    ) pg'

SELECT 
      d.name
    , total_size_mb = data.row_size_mb + data.log_size_mb
    , data.log_size_mb
    , data.row_size_mb
    , reserved_space_mb = CAST(do.reservedpages * 8. / 1024 AS DECIMAL(10,2))
    , data_size_mb = CAST(do.pages * 8. / 1024 AS DECIMAL(10,2))
    , index_size_mb = CAST((do.usedpages - do.pages) * 8. / 1024 AS DECIMAL(10,2))
    , unused_size_mb = CAST((do.reservedpages - do.usedpages) * 8. / 1024 AS DECIMAL(10,2))
    , unallocated_space_mb = 
        CAST(CASE WHEN data.row_size >= do.reservedpages 
            THEN (data.row_size - do.reservedpages) * 8. / 1024 
            ELSE 0
        END AS DECIMAL(10,2))
FROM (
    SELECT
          df2.database_id
        , log_size_mb = CAST(df2.log_size * 8. / 1024 AS DECIMAL(10,2))
        , row_size_mb = CAST(df2.row_size * 8. / 1024 AS DECIMAL(10,2)) 
        , df2.log_size
        , df2.row_size
    FROM (
        SELECT 
              df.database_id
            , log_size = SUM(CASE WHEN df.type_desc = 'LOG' THEN df.size END)
            , row_size = SUM(CASE WHEN df.type_desc = 'ROWS' THEN df.size END)
        FROM sys.master_files df
        GROUP BY df.database_id 
    ) df2
) data
JOIN sys.databases d ON data.database_id = d.database_id
JOIN #DBObj do ON do.DB = d.name
ORDER BY data.row_size_mb + data.log_size_mb DESC
2021-03-08