欲知道當前資料庫空間使用大小,可以從下麵這個系統表sys.database_files 得到。 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- -- Author: Insus.NET -- Create date: 2019-05-14 -- ...
欲知道當前資料庫空間使用大小,可以從下麵這個系統表sys.database_files 得到。
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Insus.NET -- Create date: 2019-05-14 -- Update date: 2019-05-14 -- Description: 資料庫空間使用 -- ============================================= CREATE FUNCTION [dbo].[svf_DB_SpaceUsed]() RETURNS @db TABLE([name] SYSNAME,[physical_name] SYSNAME,[type_desc] SYSNAME,[Size(byte)] bigint) AS BEGIN INSERT INTO @db ([name],[physical_name],[type_desc],[Size(byte)]) SELECT [name],[physical_name],[type_desc],SUM(CAST(FILEPROPERTY(name, 'SpaceUsed') AS bigint) * 8192.) FROM sys.database_files GROUP BY [name],[physical_name],[type_desc] RETURN END GOSource Code
運行函數:
以上算出大小單位為byte,如果你需要MB或GB作為單位呢?
MB:
SUM(CAST(FILEPROPERTY(name, 'SpaceUsed') AS bigint) * 8192.)/1024/1024
GB:
SUM(CAST(FILEPROPERTY(name, 'SpaceUsed') AS bigint) * 8192.)/1024/1024/1024
當然還可以算出其它相關單位的值。