Sql server 表使用的空间
该程序是否准确显示了数据库中使用的空间?我对结果表示怀疑Sql server 表使用的空间,sql-server,tsql,sql-server-2008-r2,Sql Server,Tsql,Sql Server 2008 R2,该程序是否准确显示了数据库中使用的空间?我对结果表示怀疑 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 OBJE
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确实有问题-今天没有格式化工具栏。您的代码提供了所用空间的逐表视图。您也可以在不使用参数的情况下运行
sp_spaceused
,以了解整个数据库的大小。什么使你怀疑结果? < P>你可能想考虑使用一个
例如,考虑这个简单的查询,用于返回堆堆和非聚集索引页的更详细的信息:
select * from sys.dm_db_index_physical_stats (
DEFAULT
, DEFAULT
, DEFAULT
, DEFAULT
, 'DETAILED'
)
感谢您提供的提示,存储过程接受表名作为参数。proc的输出与上面的输出匹配。我对此表示怀疑,因为在将KB转换为MB后,使用的表空间总和超过了db大小。