SQL server obtains the disk usage of each table in the database.

Source: Internet
Author: User
Tags rowcount
Create table tmp (name varchar (500), rows int, reserved varchar (500 ),
Data varchar (500), index_size varchar (500), unused varchar (500 ))
Insert into tmp (name, rows, reserved,
Data, index_size, unused) exec sp_msforeachTable @ Command1 = "sp_spaceused '? '"-- Sp_spaceused 't_ vehicle'
Select * from tmp order by [rows] desc
Drop table tmp

Or I can write it myself, but only the number of rows does not use the system stored procedure 1 declare @ maxrowcount int
2 declare @ maxtablename varchar (200)
3 declare @ SQL nvarchar (2000)
4 declare @ tempcount int
5 set @ SQL = '';
6 set @ maxtablename = '';
7 create table # rowcount
8 (counts int, tname varchar (200 ))
9 declare Tnamecursor cursor for select name from sysobjects where xtype = 'U' and OBJECTPROPERTY (id, N 'isusertable') = 1
10 declare @ name varchar (200 );
11 open Tnamecursor
12 fetch next from Tnamecursor INTO @ name
13 WHILE @ FETCH_STATUS = 0
14 BEGIN
15 set @ SQL = 'select @ tempcount = count (*) from '+ @ name + 'insert into # rowcount values (@ tempcount, @ name )';
16 print @ SQL
17 exec sp_executesql @ SQL, n' @ tempcount int, @ name varchar (200) ', @ tempcount, @ name
18 -- print @ name + 'total count: '+ Convert (varchar (200), @ tempcount)
19 if (@ maxrowcount <@ tempcount)
20 begin
21 set @ maxrowcount = @ tempcount;
22 set @ maxtablename = @ name;
23 end
24 fetch next from Tnamecursor INTO @ name
25 end
26 CLOSE Tnamecursor
27 DEALLOCATE Tnamecursor
28
29 select * from # rowcount order by counts desc
30
31 drop table # rowcount
32
33
34
35
36
37

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.