Link to home
Start Free TrialLog in
Avatar of dbdoshi
dbdoshi

asked on

List tables in SQL Server 7

Hi, I want to write a query to list all tables in a particular database having 2 columns

Name | No. of Rows.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of JamesT
JamesT

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brendt Hess
You will need to use a cursor and some dynamic SQL coding to implement this, I'm afraid:

Declare @tblName nvarchar(128)
Declare c_Tables CURSOR For
   SELECT RTrim(Name) FROM sysobjects WHERE xtype = 'U'

-- Use a global temp table for the procedure
Create Table ##TableList (TableName nvarchar(128), NumRows int)

-- Open the cursor
Open c_Tables
Fetch Next from c_Tables INTO @tblName
While @@FETCH_STATUS = 0
BEGIN
   Exec('INSERT INTO ##TableList SELECT  ''' + @tblName + ''', Count(*) As NumRows FROM ' + @tblName')
   FETCH NEXT FROM c_Tables INTO @tblName
END
Close c_Tables
Deallocate c_tables
SELECT * FROM ##TableList ORDER BY TableName
Drop ##TableList
I think this will work:

SELECT OBJECT_NAME(id), name
FROM syscolumns
WHERE id IN
(SELECT sysc.id
FROM syscolumns sysc
INNER JOIN sysobjects syso ON sysc.id = syso.id AND syso.xtype = 'U'
GROUP BY sysc.id
HAVING COUNT(*) = 2)
Sorry, ignore last posting, mis-read requirement.
Avatar of chigrik
chigrik

Try this:

USE Northwind
select a.name, c.rows, count(b.name) as columns
from sysobjects a inner join syscolumns b on a.id = b.id join sysindexes c on a.id = c.id
where a.type = 'u' and c.indid < 2
group by a.name, c.rows having count(b.name) = 2


Result set:

name                           rows        columns    
------------------------------ ----------- -----------
CustomerCustomerDemo           0           2
CustomerDemographics           0           2
Region                         4           2
EmployeeTerritories            49          2

(4 row(s) affected)


With best regards,
Alexander Chigrik
http://www.MSSQLCity.com/ - all about MS SQL
(SQL Server Articles, FAQ, Scripts, Tips and Test Exams).
Avatar of dbdoshi

ASKER

James T,

   I ran your query. Also, I got another answer from microsoft newsgroup. It said:

Select O.Name, I.rows
From sysobjects O Inner Join sysindexes I
On O.ID = I.ID AND O.Name = I.Name
Where o.xtype = 'u'

Now, the problem is, my database has more than 500 tables, so its impossible to see which query is returning what. Can you explain whats wrong with the above query so that I can understand it. Thank you very much.
Avatar of dbdoshi

ASKER

chigrik,

    I need all tables and their no of rows, I think you misunderstood the question (I don't want tables with just 2 columns), I need a resultset of 2 columns, one of which is the table name and other is the no. of rows in that table. Thanks.
I don't understand the question. What is wrong with the Query?
Avatar of dbdoshi

ASKER

The thing is, the other query is returning like 450 rows, and your query returns around 500, so I was just wondering if you can explain a bit more on the logic behind that. Thanks.
This modification will give you the same results as the one from the MS Newsgroup:

select so.name as Name, si.rows as Rows
from sysindexes si
inner join sysobjects so
on so.id = si.id and si.name = so.name
where so.xtype = 'U' and si.indid in (0,1)
order by 1
Try this:

USE Northwind
select a.name, c.rows
from sysobjects a join sysindexes c on a.id = c.id
where a.type = 'u' and c.indid < 2

Result set:

"
name                               rows        
---------------------------------  -----------
Orders                             830
Products                           77
Order Details                      2155
CustomerCustomerDemo               0
CustomerDemographics               0
Region                             4
Territories                        53
EmployeeTerritories                49
Employees                          9
Categories                         8
Customers                          91
Shippers                           3
Suppliers                          29
"


With best regards,
Alexander Chigrik
http://www.MSSQLCity.com/ - all about MS SQL
(SQL Server Articles, FAQ, Scripts, Tips and Test Exams).
Avatar of dbdoshi

ASKER

Thank you very much. I appreciate your help.