Solved

List tables in SQL Server 7

Posted on 2002-04-05
12
1,391 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: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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

785 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