How to get a sql query which will get me all the tables with their respect row counts within one schema
-
[Oracle Metadata](http://en.wikipedia.org/wiki/Oracle_metadata#Example_3:_counting_rows_of_columns) – tyh Nov 17 '13 at 21:20
1 Answers
Below is the Microsoft SQL Server query to retrieve all Tables what the schema is for the table and the amount of rows.
to use for specific schema simply uncomment the last row of there where clause(remove /**/). and enter the schema names that you are looking for in the brackets on the last row of the where clause you can enter more then 2 just simply follow the pattern 'SchemaName','SchemaName','SchemaName'.
SELECT
t.NAME AS TableName,
s.name AS SchemaName,
p.[Rows]
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.schemas s on s.schema_id = t.schema_id
WHERE
t.NAME NOT LIKE 'dt%' AND
i.OBJECT_ID > 255 AND
i.index_id <= 1 /* AND
s.name in ('Schemaname1','Schemaname2') */
GROUP BY
t.NAME, i.object_id, i.index_id, i.name,s.name, p.[Rows]
ORDER BY
object_name(i.object_id)
if you would like to query like this
SELECT owner, table_name, NUM_ROWS FROM ALL_TABLES where owner like 'Schema_name'
I would suggest creating a view like so.
CREATE VIEW ALL_TABLES
AS
SELECT
t.NAME AS table_name,
s.name AS owner,
p.[Rows] as NUM_ROWS
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.schemas s on s.schema_id = t.schema_id
WHERE
t.NAME NOT LIKE 'dt%' AND
i.OBJECT_ID > 255 AND
i.index_id <= 1 /* AND
s.name in ('Schemaname1','Schemaname2') */
GROUP BY
t.NAME, i.object_id, i.index_id, i.name,s.name, p.[Rows]
ORDER BY
object_name(i.object_id)
and then if you run the following query it will work
SELECT owner, table_name, NUM_ROWS FROM ALL_TABLES where owner like 'Schema_name'
If this helps please mark the answer as correct

- 355
- 1
- 11
-
SELECT owner, table_name, NUM_ROWS FROM ALL_TABLES where owner like 'Schema_name' Will this work? – user3002581 Nov 17 '13 at 21:29
-
I'm sorry but this is just wrong... the OP was asking for Oracle and this will not work on Oracle. It also won't work on SQL Server; as [the documentation says](http://technet.microsoft.com/en-us/library/ms175012.aspx) `sys.partitions.rows` _"Indicates the approximate number of rows in this partition."_, so it won't be accurate... – Ben Nov 17 '13 at 21:47
-
I'm not sure how you can say the OP would like it for Oracle as the question was really vague. sys.partitions.rows is the easiest way this can be done in SQL. – Luke Franklin Nov 17 '13 at 21:53
-
Because that's what the tags are, and the OPs code in the comment above... it may be the easiest way in SQL, in SQL Server, but it's also inexact. If you're going to recommend something that won't give an accurate answer then it might be better to say so. – Ben Nov 17 '13 at 21:59
-
SQL is a **query language** it is *not* a DBMS product. Therefor the tag `sql` refers to that language and not to the DBMS made by Microsoft (which *is* explained in the tag's short description) – Nov 17 '13 at 22:35