udb992002

把数据表按照从大到小排列出来

IF EXISTS(SELECT name FROM sysobjects WHERE name=\'usp_get_spaceused\' and type=\'p\')
BEGIN
 DROP PROCEDURE usp_get_spaceused
END

GO

CREATE PROCEDURE usp_get_spaceused
AS
BEGIN

 declare @id       int                  -- The object id of @objname. 
 declare @type       character(2) -- The object type. 
 declare       @pages       int                  -- Working variable for size calc. 
 declare @dbname sysname 
 declare @dbsize dec(15,0) 
 declare @logsize dec(15) 
 declare @bytesperpage       dec(15,0) 
 declare @pagesperMB              dec(15,0) 
 declare @objname nvarchar(776)        -- The object we want size on. 
 declare @UPDATEusage varchar(5)             -- Param. for specifying that

 CREATE TABLE #temp1
 (
 
        表名              varchar(200) null,
 
        行数               char(11) null,
 
        保留空间         varchar(15) null,
 
        数据使用空间       varchar(15) null,
 
        索引使用空间       varchar(15) null,
 
         未用空间          varchar(15) null
 
 )
 
 SELECT @UPDATEusage=\'false\'
 
 DECLARE cur_table CURSOR FOR
 SELECT NAME FROM SYSOBJECTS WHERE type=\'u\'
 
 OPEN cur_table
 FETCH NEXT FROM cur_table INTO @objname
 
 While @@FETCH_STATUS=0
 BEGIN
 
  CREATE TABLe #spt_space
  
  (
         rows              int null,
         reserved    dec(15) null,
         data        dec(15) null,
         indexp             dec(15) null,
         unused             dec(15) null
  
  )
  
  
  IF @UPDATEusage IS NOT NULL
         BEGIN
                SELECT @UPDATEusage=lower(@UPDATEusage)
                IF @UPDATEusage not in (\'true\',\'false\')
                       BEGIN
                              RAISERROR(15143,-1,-1,@UPDATEusage)
                              RETURN(1)
                       END
         END
  
  IF @objname IS NOT NULL
  BEGIN
         SELECT @dbname = parsename(@objname, 3)
         IF @dbname IS NOT NULL AND @dbname <> db_name()
                BEGIN
                       RAISERROR(15250,-1,-1)
                       RETURN (1)
                END
         IF @dbname IS NULL
                SELECT @dbname = db_name()
         SELECT @id = null
  
         SELECT @id = id, @type = xtype
                FROM SYSOBJECTS
                       WHERE ID = object_id(@objname)
         IF @id is null
                BEGIN
                       RAISERROR(15009,-1,-1,@objname,@dbname)
                       RETURN (1)
                END
  
         IF NOT EXISTS (SELECT * FROM SYSINDEXES
                              WHERE @id = id and indid < 2)
                IF      @type in (\'P \',\'D \',\'R \',\'TR\',\'C \',\'RF\') --data stored in sysprocedures
                              BEGIN
                                     RAISERROR(15234,-1,-1)
                                     RETURN (1)
                             END
                ELSE IF @type = \'V \' -- View => no physical data storage.
                              BEGIN
                                     RAISERROR(15235,-1,-1)
                                     RETURN (1)
                              END
                ELSE IF @type in (\'PK\',\'UQ\') -- no physical data storage. --?!?! too many similar messages
                              BEGIN
                                     RAISERROR(15064,-1,-1)
                                     RETURN (1)
                              END
                ELSE IF @type = \'F \' -- FK => no physical data storage.
                              BEGIN
                                     RAISERROR(15275,-1,-1)
                                     RETURN (1)
                              END
  
  END
  
  
  IF @UPDATEusage = \'true\'
         BEGIN
  
                IF @objname IS NULL
                       DBCC UPDATEUSAGE(0) WITH NO_INFOMSGS
                ELSE
                       DBCC UPDATEUSAGE(0,@objname) WITH NO_INFOMSGS            
         END
  
  SET NOCOUNT ON
  
  
  IF @id IS NULL
  BEGIN
         SELECT @dbsize = sum(convert(dec(15),size))
                FROM dbo.sysfiles
                WHERE (status & 64 = 0)
  
         SELECT @logsize = sum(convert(dec(15),size))
                FROM dbo.sysfiles
                WHERE (status & 64 <> 0)
  
         SELECT @bytesperpage = low
                FROM master.dbo.spt_values
                WHERE number = 1
                       and type = \'E\'
  
         SELECT @pagesperMB = 1048576 / @bytesperpage
  
         SELECT  database_name = db_name(),
                database_size =
                       ltrim(str((@dbsize + @logsize) / @pagesperMB,15,2) + \' MB\'),
                \'unallocated space\' =
                       ltrim(str((@dbsize -
                              (SELECT sum(convert(dec(15),reserved))
                                     FROM sysindexes
                                            WHERE indid in (0, 1, 255)
                              )) / @pagesperMB,15,2)+ \' MB\')
  
  
         insert into #spt_space (reserved)
                SELECT sum(convert(dec(15),reserved))
                       FROM sysindexes
                              WHERE indid in (0, 1, 255)
  
  
         SELECT @pages = sum(convert(dec(15),dpages))
                       FROM sysindexes
                              WHERE indid < 2
  
         SELECT @pages = @pages + isnull(sum(convert(dec(15),used)), 0)
                FROM sysindexes
                       WHERE indid = 255
  
         UPDATE #spt_space
                set data = @pages
  
         UPDATE #spt_space
                set indexp = (SELECT sum(convert(dec(15),used))
                              FROM sysindexes
                                     WHERE indid in (0, 1, 255))
                           - data
  
         UPDATE #spt_space
                SET unused = reserved
                              - (SELECT sum(convert(dec(15),used))
                                     FROM sysindexes
                                            WHERE indid in (0, 1, 255))
  
         SELECT reserved = ltrim(str(reserved * d.low / 1024.,15,0) +
                              \' \' + \'KB\'),
                data = ltrim(str(data * d.low / 1024.,15,0) +
                              \' \' + \'KB\'),
                index_size = ltrim(str(indexp * d.low / 1024.,15,0) +
                              \' \' + \'KB\'),
                unused = ltrim(str(unused * d.low / 1024.,15,0) +
                              \' \' + \'KB\')
                FROM #spt_space, master.dbo.spt_values d
                WHERE d.number = 1
                       and d.type = \'E\'
  
  END
  
  ELSE
  
  BEGIN
         insert into #spt_space (reserved)
                SELECT sum(reserved)
                       FROM sysindexes
                              WHERE indid in (0, 1, 255)
                                     and id = @id
         SELECT @pages = sum(dpages)
                       FROM sysindexes
                              WHERE indid < 2
                                     and id = @id
  
         SELECT @pages = @pages + isnull(sum(used), 0)
                FROM sysindexes
                       WHERE indid = 255
                              and id = @id
  
         UPDATE #spt_space
                set data = @pages
  
  
         UPDATE #spt_space
                set indexp = (SELECT sum(used)
                              FROM sysindexes
                                     WHERE indid in (0, 1, 255)
                                            and id = @id)
                           - data
  
         UPDATE #spt_space
                SET unused = reserved
                              - (SELECT sum(used)
                                     FROM sysindexes
                                            WHERE indid in (0, 1, 255)
                                                   and id = @id)
  
         UPDATE #spt_space
                set rows = i.rows
                       FROM sysindexes i
                              WHERE i.indid < 2
                                     and i.id = @id
  
          iNSERT INTO #temp1
         SELECT name = object_name(@id),
                rows = convert(char(11), rows),
                reserved = ltrim(str(reserved * d.low / 1024.,15,0) +
                              \' \' + \'KB\'),
                data = ltrim(str(data * d.low / 1024.,15,0) +
                              \' \' + \'KB\'),
                index_size = ltrim(str(indexp * d.low / 1024.,15,0) +
                              \' \' + \'KB\'),
                unused = ltrim(str(unused * d.low / 1024.,15,0) +
                              \' \' + \'KB\')
         FROM #spt_space, master.dbo.spt_values d
                WHERE d.number = 1
                       and d.type = \'E\'
   DROP TABLE #spt_space
  END
  
  FETCH NEXT FROM cur_table INTO @objname
 END
 
 Close cur_table
 
 DEALLOCATE cur_table
 
 SELECT * FROM #temp1 ORDER BY len(保留空间) desc,保留空间 desc
 
 DROP TABLE #TEMP1
 
 RETURN (0)

END

 

分类:

技术点:

相关文章:

  • 2022-02-01
  • 2022-12-23
  • 2021-09-21
  • 2021-07-13
  • 2022-01-22
  • 2021-12-26
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案