Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

List tables in SQL Server 7

Posted on 2002-04-05
12
Medium Priority
?
1,457 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 70

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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