计算SQL Server数据库中的表数[重复]

时间:2021-05-17 01:05:25

This question already has an answer here:

这个问题在这里已有答案:

I have a SQL Server 2012 database called MyDatabase. How can I find how many tables are in the database?

我有一个名为MyDatabase的SQL Server 2012数据库。如何查找数据库中有多少个表?

I'm assuming the format of the query would be something like the following, but I don't know what to replace database_tables with:

我假设查询的格式类似于以下内容,但我不知道用以下内容替换database_tables:

USE MyDatabase
SELECT COUNT(*)
FROM [database_tables]

3 个解决方案

#1


17  

You can use INFORMATION_SCHEMA.TABLES to retrieve information about your database tables.

您可以使用INFORMATION_SCHEMA.TABLES来检索有关数据库表的信息。

As mentioned in the Microsoft Tables Documentation:

如Microsoft Tables文档中所述:

INFORMATION_SCHEMA.TABLES returns one row for each table in the current database for which the current user has permissions.

INFORMATION_SCHEMA.TABLES为当前用户具有权限的当前数据库中的每个表返回一行。

The following query, therefore, will return the number of tables in the specified database:

因此,以下查询将返回指定数据库中的表数:

USE MyDatabase
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

As of SQL Server 2008, you can also use sys.tables to count the the number of tables.

从SQL Server 2008开始,您还可以使用sys.tables来计算表的数量。

From the Microsoft sys.tables Documentation:

从Microsoft sys.tables文档:

sys.tables returns a row for each user table in SQL Server.

sys.tables为SQL Server中的每个用户表返回一行。

The following query will also return the number of table in your database:

以下查询还将返回数据库中的表的数量:

SELECT COUNT(*)
FROM sys.tables

#2


3  

Try this:

尝试这个:

SELECT Count(*)
FROM <DATABASE_NAME>.INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

#3


1  

USE MyDatabase
SELECT Count(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';

to Get Table Counts

得到表计数

  SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'dbName';

this also works

这也行得通

   USE databasename; SHOW TABLES; SELECT FOUND_ROWS();

#1


17  

You can use INFORMATION_SCHEMA.TABLES to retrieve information about your database tables.

您可以使用INFORMATION_SCHEMA.TABLES来检索有关数据库表的信息。

As mentioned in the Microsoft Tables Documentation:

如Microsoft Tables文档中所述:

INFORMATION_SCHEMA.TABLES returns one row for each table in the current database for which the current user has permissions.

INFORMATION_SCHEMA.TABLES为当前用户具有权限的当前数据库中的每个表返回一行。

The following query, therefore, will return the number of tables in the specified database:

因此,以下查询将返回指定数据库中的表数:

USE MyDatabase
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

As of SQL Server 2008, you can also use sys.tables to count the the number of tables.

从SQL Server 2008开始,您还可以使用sys.tables来计算表的数量。

From the Microsoft sys.tables Documentation:

从Microsoft sys.tables文档:

sys.tables returns a row for each user table in SQL Server.

sys.tables为SQL Server中的每个用户表返回一行。

The following query will also return the number of table in your database:

以下查询还将返回数据库中的表的数量:

SELECT COUNT(*)
FROM sys.tables

#2


3  

Try this:

尝试这个:

SELECT Count(*)
FROM <DATABASE_NAME>.INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

#3


1  

USE MyDatabase
SELECT Count(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE';

to Get Table Counts

得到表计数

  SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'dbName';

this also works

这也行得通

   USE databasename; SHOW TABLES; SELECT FOUND_ROWS();