views:

895

answers:

5

How would i get the number of fields/entries in a database using an SQL Statement?

Thanks, Ash

+4  A: 

mmm all the fields in all the tables? assuming standards (mssql, mysql, postgres) you can issue a query over information_schema.columns

  SELECT COUNT(*) 
  FROM INFORMATION_SCHEMA.COLUMNS

Or grouped by table:

  SELECT TABLE_NAME, COUNT(*) 
  FROM INFORMATION_SCHEMA.COLUMNS 
  GROUP BY TABLE_NAME
Jhonny D. Cano -Leftware-
FYI that will also include views
SQLMenace
In any case, he doesn't specified the engine, and that behavior could be specific of mssql.
Jhonny D. Cano -Leftware-
both MySQL and PostgreSQL have table_type in information_schema.tables
SQLMenace
Information schema is an ANSI standard. http://en.wikipedia.org/wiki/Information_Schema
Jeff Meatball Yang
+1  A: 

try this, this will exclude views, leave the where clause out if you want views

  select count(*) from information_schema.columns c
join information_schema.tables t on c.table_name = t.table_name
and t.table_type = 'BASE TABLE'
SQLMenace
A: 

Sounds like this is what you need.

select CountOfFieldsInDatabase = count(*)
from   information_schema.columns
Scott Ivey
That's not counting fields (columns). It's counting rows.
Ken White
its counting ROWS in the table that describes the columns. One row per column in the database.
Scott Ivey
@Ken: No, it's definitely counting the number of columns across all tables in the DB. (Which is a slightly useless thing to want to do, but it is what the OP asked for.)
j_random_hacker
Ken, each row will be for one column
SQLMenace
+1  A: 

just count the columns yourself or use the calculator

Chidambaram
A: 

select count(column_name) from information_schema.columns where table_name = *name of your table here *

hye ji