MSDN information: https://msdn.microsoft.com/zh-cn/library/ms188776.aspx
If you just query the size of the database, you can use the following statement directly:
EXEC sp_spaceused
In order to ensure real-time query results, it is recommended to use the @updateusage parameter to ensure that the statistics are up-to-date:
EXEC sp_spaceused @updateusage = N'TRUE';
After the execution is completed, the result is two tables, the first table contains basic statistical information, and the second table shows more detailed data occupancy.
If you want to specifically query the size of a table, just add the table name:
EXEC sp_spaceused 'spt_values'
The result is simple:
(over)
。。。
In fact, if you can look up a table, you just want to figure out how to look up all the tables in the database. . .
Well, let's have one, found on the Internet:
-- =============================================== --Description _ : Update the size of each table in the query database, and store the results in the data table -- ================================= ============= CREATE PROCEDURE [ dbo ] . [ sp_UpdateTableSpaceInfo ] AS BEGIN -- Query whether the result store table exists IF NOT EXISTS ( SELECT * FROM sysobjects where id = OBJECT_ID (N ' temp_tableSpaceInfo ' ) AND OBJECTPROPERTY (id, N ' IsUserTable ') = 1 ) BEGIN -- create CREATE TABLE temp_tableSpaceInfo if it does not exist (name NVARCHAR(128), rows char(11), reserved VARCHAR(18), data VARCHAR(18), index_size VARCHAR(18), unused VARCHAR ( 18 )) END -- empty data table DELETE FROM temp_tableSpaceInfo --Define a temporary variable to store the table name when traversing DECLARE @tablename VARCHAR ( 255 ) --Use the cursor to read all the table table names in the database DECLARE table_list_cursor CURSOR FOR SELECT name FROM sysobjects WHERE OBJECTPROPERTY (id, N ' IsTable ' ) = 1 AND name NOT LIKE N ' #%% ' ORDER BY name --Open the cursor OPEN table_list_cursor --Read the first data FETCH NEXT FROM table_list_cursor INTO @tablename -- Traverse the queried table name WHILE @@FETCH_STATUS = 0 BEGIN -- Check whether the current table is a user table IF EXISTS ( SELECT * FROM sysobjects WHERE id = OBJECT_ID ( @tablename ) AND OBJECTPROPERTY (id, N ' IsUserTable ' ) = 1 ) BEGIN -- the current table reads its information and inserts it into the table EXECUTE sp_executesql N ' INSERT INTO temp_tableSpaceInfo EXEC sp_spaceused @tbname ' , N '@tbname varchar(255) ' , @tbname = @tablename END -- read the next data FETCH NEXT FROM table_list_cursor INTO @tablename END --Release the cursor CLOSE table_list_cursor DEALLOCATE table_list_cursor END GO
When using it, execute:
EXEC sp_UpdateTableSpaceInfo SELECT * FROM temp_tableSpaceInfo
Well, it should be fine.