Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

user tables in database where the number of rows is less than 100 Expand / Collapse
Author
Message
Posted Sunday, January 11, 2009 2:46 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, June 03, 2011 4:58 PM
Points: 88, Visits: 191
Hello Team,

I am trying to find out the name of the user tables in my database where the number of rows is less than 100. I am using following command to find this.

I am not good at writing queries. Can you guide me how to become proficient in writing good sql queries. Query as follows can give you a glimplse as how bad I am in writing the sql queries.

select n.name, n.count(1)
(
select count(1) from name
)
from sysobjects n where xtype = 'u'
and n.count(1) < 100

Regards,
Michael


-MJ
Please do not print mails and docx unless it is absolutely necessary. Spread environmental awareness.
Post #634414
Posted Sunday, January 11, 2009 4:42 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 10, 2010 5:07 AM
Points: 2,732, Visits: 23,078
For a quick solution, you can check the row count of the indexes on the tables rather than counting the actual rows.

SELECT
SO.Name
, SI.RowCnt
FROM
SysObjects SO
LEFT JOIN SysIndexes SI ON SO.ID = SI.ID AND SI.IndID = 1
WHERE
SO.Type = 'u'
AND (SI.RowCnt IS NULL OR SI.RowCnt < 100)


This will return the tables with less than 100 rows and any tables without any indexes will have a rowcount of NULL.

Post #634425
Posted Sunday, January 11, 2009 7:36 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 9:21 PM
Points: 2,842, Visits: 2,423
A more accurate query is...
SELECT
SO.Name
, SI.RowCnt
FROM
SysObjects SO
INNER JOIN SysIndexes SI ON SO.ID = SI.ID
WHERE
SO.Type = 'u'
AND (SI.RowCnt IS NULL OR SI.RowCnt < 100) -- nto 100% sure but I think rowcnt will never be null
and SI.IndID IN (0, 1)

A table will only have one record in SYSINDEXES with a value of either 0 or 1. This simply depends on whether (or not) the table has a clustered index.



Post #634449
Posted Monday, January 12, 2009 4:15 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 10, 2010 5:07 AM
Points: 2,732, Visits: 23,078
happycat - you would need to move your additional criteria into the join.

SELECT
SO.Name
, SI.RowCnt
FROM
SysObjects SO
INNER JOIN SysIndexes SI ON SO.ID = SI.ID and SI.IndID IN (0, 1)
WHERE
SO.Type = 'u'
AND (SI.RowCnt IS NULL OR SI.RowCnt < 100) -- nto 100% sure but I think rowcnt will never be null


The NULL condition is to handle tables with no indexes. I wanted to make sure the query did not ignore heaps, so it returns rows with no sysindexes entries.
Post #634554
Posted Tuesday, January 13, 2009 12:42 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, April 18, 2014 1:55 AM
Points: 911, Visits: 682
select distinct object_name(object_id) as Table, rows
from sys.partitions where index_id >= 1
and object_name(object_id) not like 'sys%'
and rows > 100


Abhijit - http://abhijitmore.wordpress.com
Post #635180
Posted Tuesday, January 13, 2009 1:02 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:30 PM
Points: 41,531, Visits: 34,448
Abhijit More (1/13/2009)
select distinct object_name(object_id) as Table, rows
from sys.partitions where index_id >= 1
and object_name(object_id) not like 'sys%'
and rows > 100


Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Table'.

Also, yours would miss tables that had no indexes at all, count system tables whos names don't start with sys (service broker queues as an example) and miss user tables that do start with sys

select object_name(object_id) as TableName, rows
from sys.partitions
where index_id in (0,1) -- heap or clustered index. No point in counting the nonclusters
and OBJECTPROPERTY(object_id, 'IsMSShipped')=0 -- Don't want any system tables, do want user tables that may start with sys
and rows < 100




Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #635187
Posted Saturday, January 17, 2009 1:49 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, June 03, 2011 4:58 PM
Points: 88, Visits: 191
Thanks so much. Somehow I am not able to run the command. I am sure that you would have got the correct result of your respective queries ...

Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.partitions'.

select @@version:
Microsoft SQL Server 2000 - 8.00.2050 (Intel X86) Mar 7 2008 21:29:56 Copyright (c) 1988-2003 Microsoft Corporation Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 1)

select * from sysobjects where name like '%partition%' --> No row is selected

Your query is good for 2005 but what should I do for 2000

-M


-MJ
Please do not print mails and docx unless it is absolutely necessary. Spread environmental awareness.
Post #638762
Posted Saturday, January 17, 2009 2:06 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:30 PM
Points: 41,531, Visits: 34,448
Please post SQL 2000-related questions in the SQL 2000 forums. If you post in the 2005 forums, you're very likely to get 2005-specific solutions. All this does is waste everyone's time.

For SQL 2000, use sysindexes. I'm not writing the entire query for you. The idea is the same as with sys.partitions, the column names differ.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #638773
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse