10

I would like to return all the tables and its count next to it. what is the quickest way to go about it?

I know in Oracle, you can do something like below, but not sure about Sybase:

declare n number;
begin
   for rec in (select object_name from user_objects where object_type='TABLE')
   loop
     execute immediate 'select count(*) from '||rec.object_name into n;
     dbms_output.put_line (rec.object_name||':'||n);
   end loop;
end;

7 Answers 7

23

Here is the Sybase sql that does the above:

select ob.name,st.rowcnt 
from sysobjects ob, systabstats st 
where ob.type="U"  
and st.id=ob.id 
order by ob.name
6
  • 1
    I am getting multiple entries for many tables by running this query. I think it might be because there are multiple partitions. Can that be the cause?
    – partha
    Jan 28, 2015 at 10:47
  • Possibly. Look at sysobjects table for more detail.
    – Neerav
    Jan 29, 2015 at 14:52
  • 2
    Added group by clause to handle multiple entries: select ob.name, sum(st.rowcnt) from sysobjects ob, systabstats st where ob.type="U" and st.id=ob.id group by ob.name order by ob.name
    – Laura
    Jul 27, 2015 at 20:47
  • 7
    Sybase IQ reacts with Table 'systabstats' not found !?! May 26, 2016 at 11:30
  • This gives an approximation not an exact count.
    – whirlwin
    Feb 17, 2020 at 20:28
11

It depends on what Sybase product you mean. In my SQL Anywhere (9 and 11), your solution doesn't work, but this works:

select table_name, count
from systable
where primary_root<>0 and creator=1
order by 1
4
  • Unfortunately, systable is deprecated May 26, 2016 at 11:18
  • I have to use an older version of sybase and this worked perfectly! Jul 8, 2016 at 19:27
  • This only displayed tables for me that have DBA as the owner. Aug 25, 2017 at 22:20
  • 1
    It works in SQL Anywhere 16 as well. I was logged in as non-DBA and it showed all the tables for that user, as I wished.
    – Erica Kane
    Oct 8, 2017 at 15:38
4

As there can be multiple entries in systabstats table, the query should be:

select ob.name, sum(st.rowcnt)
from sysobjects ob, systabstats st 
where ob.type="U"  
and st.id=ob.id 
group by ob.name
order by ob.name
0
3

If the current user is the creator:

SELECT table_name, count
  FROM sys.systable
 WHERE creator = user_id()

NOTE: I test this in Sybase ASA 9.

1
  • Likewise, this only displayed tables for me that have DBA as the owner. Aug 25, 2017 at 22:49
0

use the below query

select name,row_count(db_id(),id) as "Rows"
from sysobjects where type='U' order by 2 desc
0

This works for me using "SQL Central" with SQL Anywhere 17:

SELECT table_name, st.count
FROM systable st 
WHERE table_type = 'BASE'
0
select ob.name,st.rowcnt from sysobjects ob, systabstats st  where  b.type='U' 
and st.id=ob.id  and indid=0 order by ob.name
2
  • 2
    Here I also get "Table 'systabstats' not found" Aug 25, 2017 at 22:51
  • 2
    Can you add some context to the answer instead of posting code-only answer? Dec 23, 2019 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.