码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server查看所有表大小,所占空间

时间:2018-07-18 10:12:29      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:har   status   next   declare   sed   for   dea   etc   begin   

CREATE TABLE #Data
(
    name VARCHAR(100),
    row VARCHAR(100),
    reserved VARCHAR(100),
    data VARCHAR(100),
    index_size VARCHAR(100),
    unused VARCHAR(100)
);

DECLARE @name VARCHAR(100);
DECLARE cur CURSOR FOR
SELECT name
FROM sysobjects
WHERE xtype = ‘u‘
ORDER BY name;
OPEN cur;
FETCH NEXT FROM cur
INTO @name;
WHILE @@fetch_status = 0
BEGIN
    INSERT INTO #Data
    EXEC sp_spaceused @name;
    PRINT @name;

    FETCH NEXT FROM cur
    INTO @name;
END;
CLOSE cur;
DEALLOCATE cur;

CREATE TABLE #DataNew
(
    name VARCHAR(100),
    row INT,
    reserved INT,
    data INT,
    index_size INT,
    unused INT
);

INSERT INTO #DataNew
SELECT name,
       CONVERT(INT, row) AS row,
       CONVERT(INT, REPLACE(reserved, ‘KB‘, ‘‘)) AS reserved,
       CONVERT(INT, REPLACE(data, ‘KB‘, ‘‘)) AS data,
       CONVERT(INT, REPLACE(index_size, ‘KB‘, ‘‘)) AS index_size,
       CONVERT(INT, REPLACE(unused, ‘KB‘, ‘‘)) AS unused
FROM #Data;

SELECT *
FROM #DataNew
ORDER BY data DESC;

DROP TABLE #Data,
           #DataNew;

SQL Server查看所有表大小,所占空间

标签:har   status   next   declare   sed   for   dea   etc   begin   

原文地址:https://www.cnblogs.com/Cruise-Yang/p/9327388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!