3

この手順は、データベースで使用されているスペースを正確に示していますか? 私はその結果を疑っています。

DECLARE @TableName VARCHAR(100)    --For storing values in the cursor

--Cursor to get the name of all user tables from the sysobjects listing
DECLARE tableCursor CURSOR
FOR 
select [name]
from dbo.sysobjects 
where  OBJECTPROPERTY(id, N'IsUserTable') = 1
FOR READ ONLY

--A procedure level temp table to store the results
CREATE TABLE #TempTable
(
    tableName varchar(100),
    numberofRows varchar(100),
    reservedSize varchar(50),
    dataSize varchar(50),
    indexSize varchar(50),
    unusedSize varchar(50)
)

--Open the cursor
OPEN tableCursor

--Get the first table name from the cursor
FETCH NEXT FROM tableCursor INTO @TableName

--Loop until the cursor was not able to fetch
WHILE (@@Fetch_Status >= 0)
BEGIN
    --Dump the results of the sp_spaceused query to the temp table
    INSERT  #TempTable
        EXEC sp_spaceused @TableName

    --Get the next table name
    FETCH NEXT FROM tableCursor INTO @TableName
END

--Get rid of the cursor
CLOSE tableCursor
DEALLOCATE tableCursor

--Select all records so we can use the reults
SELECT * 
FROM #TempTable order BY tablename

--Final cleanup!
DROP TABLE #TempTable

この投稿の形式について申し訳ありません。StackO には確かにバグがあります。現在、書式設定ツールバーはありません。

4

2 に答える 2

1

あなたのコードは、使用されたスペースのテーブルごとのビューを提供します。sp_spaceusedデータベース全体のサイズの概要を取得するために、params なしで実行することもできます。結果を疑う理由は何ですか?

于 2012-05-10T15:20:13.953 に答える
1

システムの動的ビュー/関数のいずれかを使用することを検討したい場合があります

たとえば、sys.dm_db_index_physical_statsを使用して、ヒープ クラスター化インデックスと非クラスター化インデックスに使用されるページに関するより詳細な情報を返す単純なクエリについて考えてみましょう。

select * from sys.dm_db_index_physical_stats ( 
    DEFAULT 
  , DEFAULT 
  , DEFAULT 
  , DEFAULT 
  , 'DETAILED'
)
于 2012-05-10T15:25:20.627 に答える