我有一个名为 MyDatabase
的 SQL Server 2012 数据库。如何找到数据库中有多少张表?
我假设查询的格式如下所示,但我不知道要替换什么 database_tables
与:
USE MyDatabase
SELECT COUNT(*)
FROM [database_tables]
最佳答案
您可以使用INFORMATION_SCHEMA.TABLES
检索有关数据库表的信息。
如 Microsoft Tables Documentation 中所述:
INFORMATION_SCHEMA.TABLES
returns one row for each table in the current database for which the current user has permissions.
因此,以下查询将返回指定数据库中的表数量:
USE MyDatabase
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
<小时/>
从 SQL Server 2008 开始,您还可以使用 sys.tables
来计算表的数量。
来自Microsoft sys.tables Documentation :
sys.tables
returns a row for each user table in SQL Server.
以下查询还将返回数据库中表的数量:
SELECT COUNT(*)
FROM sys.tables
关于sql-server - 计算 SQL Server 数据库中的表数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45464661/