SQL Server obtains the names and data entries of all tables in the database.

Source: Internet
Author: User

When we mention the number of data entries in a single table, we will think of select count (*) from tablename

What if we want to get the number of all tables in the database? Let's look at several of the most common methods:

-- Method 1 if exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [tablespace] ') and objectproperty (ID, N 'isusertable') = 1) Drop table [DBO]. [tablespace] gocreate table tablespace (tablename varchar (20), rowscount char (11), reserved varchar (18), data varchar (18), index_size varchar (18 ), unused varchar (18) godeclare @ SQL varchar (500) Declare @ tablename varchar (20) Declare mcursor curs Orforselect name from sysobjects where xtype = 'U' open mcursorfetch next from mcursor into @ tablenamewhile @ fetch_status = 0 begin set @ SQL = 'insert into tablespace' set @ SQL = @ SQL + 'exec sp_spaceused ''' + @ tablename + ''' exec (@ SQL) fetch next from mcursor into @ tablename endclose mcursordeallocate mcursorgo -- display the result select tablename, rowscount from tablespace -- method 2 select B. name as tablename, C. row_count as datacountfrom sys. indexes A, sys. objects B, sys. dm_db_partition_stats cwhere. [object_id] = B. [object_id] and B. [object_id] = C. [object_id] and. index_id = C. index_id and. index_id <2 and B. is_ms_shipped = 0 -- Method 3 select B. name as tablename,. rowcnt as datacountfrom sysindexes A, sysobjects bwhere. id = B. ID and. indid <2 and objectproperty (B. ID, 'ismsshipped ') = 0 -- recommended The last two methods are used. For SQL Server 2005, the three methods are good. If it is another board, you can test them one by one.

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.