?
Solved

List tables in SQL Server 7

Posted on 2002-04-05
12
Medium Priority
?
1,442 Views
Last Modified: 2011-09-20
Hi, I want to write a query to list all tables in a particular database having 2 columns

Name | No. of Rows.

Thanks.
0
Comment
Question by:dbdoshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 2

Accepted Solution

by:
JamesT earned 200 total points
ID: 6921283
select so.name as Name, si.rows as Rows
from sysindexes si
inner join sysobjects so
on so.id = si.id
where so.type = 'U' and si.indid in (0,1)
order by si.rows desc, si.name asc
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 6921389
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
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 6921524
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)
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 6921531
Sorry, ignore last posting, mis-read requirement.
0
 
LVL 8

Expert Comment

by:chigrik
ID: 6921556
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).
0
 

Author Comment

by:dbdoshi
ID: 6921572
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.
0
 

Author Comment

by:dbdoshi
ID: 6921583
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.
0
 
LVL 2

Expert Comment

by:JamesT
ID: 6921906
I don't understand the question. What is wrong with the Query?
0
 

Author Comment

by:dbdoshi
ID: 6921975
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.
0
 
LVL 2

Expert Comment

by:JamesT
ID: 6922002
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
0
 
LVL 8

Expert Comment

by:chigrik
ID: 6922400
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).
0
 

Author Comment

by:dbdoshi
ID: 6922835
Thank you very much. I appreciate your help.
0

Featured Post

The Ideal Solution for Multi-Display Applications

Check out ATEN’s VS1912 12-Port DP Video Wall Media Player at InfoComm 2017. Kerri describes how easy it is to design creative video walls in asymmetric layouts and schedule detailed playlists ahead of time with its advanced scheduling feature.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question