最近客戶的資料庫暴漲,一開始找不到原因,用sql查詢表的空間大小,結果之前用的sql語句只能查詢除了text,image外的欄位大小.搞得一時間混亂不堪.後來諮詢了大師們,找到用sp_spaceused這個系統函數可以查詢表的真實大小,包含text等欄位.sql如下:
declare @id NVARCHAR(100)create table #spt_space( [name] NVARCHAR(50) null, [rows] int null, [reserved] NVARCHAR(50) null, [data] NVARCHAR(50) null, [index_size] NVARCHAR(50) null, [unused] NVARCHAR(50) null)set nocount ondeclare c_tables cursor forselect name from sysobjects where xtype = 'U'open c_tables fetch next from c_tables into @idwhile @@fetch_status = 0begin /* Code from sp_spaceused */ insert into #spt_space ([name],[rows],reserved,data,index_size,unused) EXEC sp_spaceused @id fetch next from c_tables into @idendSELECT * FROM(SELECT *,CAST(REPLACE(reserved,'KB','') AS INT) AS reservedindex FROM #spt_space) corder by c.reservedindex descdrop table #spt_spaceclose c_tablesdeallocate c_tables