Solved

List tables in SQL Server 7

Posted on 2002-04-05
12
1,369 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
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 2

Accepted Solution

by:
JamesT earned 50 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:bhess1
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:ScottPletcher
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
 
LVL 69

Expert Comment

by:ScottPletcher
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now