I got a request from a user and he wanted to count the number of tables in a database. It's quiet simple. Just use the information_schema.tables
USE YOURDBNAME
SELECT COUNT(*) from information_schema.tables
WHERE table_type = 'base table'
' Will return you the count of the tables in the database
This SQL Server 2005 is Easy to use
ReplyDeletemore feature include in microsoft.
Nice Tip!! :-D
ReplyDeleteIt's works fine!
Thanks!
Thanks for the query. Its not working for sample databases such as AdventureWorks db.
ReplyDeleteError: Invalid object name 'information_schema.tables'.
Prawin:
ReplyDeleteIt works.
USE AdventureWorks
SELECT COUNT(*) from information_schema.tables
WHERE table_type = 'base table'
Thank you,
ReplyDeleteit works & the sql is not complicated :-)
Gracias por el query, funciona bien :) pero deberias especificar que solo cambien el nombre de la BD y nada mas el resto no se toca.
ReplyDeleteUSE NombredelaBasedeDatos
SELECT COUNT(*) from information_schema.tables
WHERE table_type = 'base table'
Alternative Query is
ReplyDeleteSELECT count(*) from sys.tables where type_desc = 'USER_TABLE'
Thanks...the information proved useful!
ReplyDeletethank you for the useful information !
ReplyDeletethanku
ReplyDeleteselect 'Owner'=convert(char(10),t.TABLE_SCHEMA),
ReplyDelete'Table Name'=convert(char(25),t.TABLE_NAME),
'Record Count'=max(i.rows)
from sysindexes i, INFORMATION_SCHEMA.TABLES t
where t.TABLE_NAME = object_name(i.id)
and t.TABLE_TYPE = 'BASE TABLE'
group by t.TABLE_SCHEMA, t.TABLE_NAME
Thanks much - very helpful
ReplyDeletethats gr8 !
ReplyDeleteI hav multiple schema in DB.
ReplyDeleteHow can i identify that result given is for that particular schema n not cumulative?
thanx a lot
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteTo get all Tables
ReplyDeleteSELECT * from information_schema.tables
To get specific schema
SELECT * from information_schema.tables WHERE TABLE_SCHEMA = 'dbo'
'dbo' is schema name
wow .. that is wonderful
ReplyDeleteSELECT sobjects.name
ReplyDeleteFROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
Above is the query that I use.
For the full list of XTYPE, you can refer:
SQL Server - Get DB Object Names, Count + XTYPE Reference
HI
ReplyDeleteI am Ashwin kumar
Thanks for this information
Count the number of tables excluding system tables.
ReplyDeleteSELECT count(*) FROM sys.objects WHERE Type_desc= 'USER_TABLE' AND is_ms_shipped <> 1