2013-08-12 14 views
6

Vorrei restituire tutti i tavoli e il relativo conteggio accanto ad esso. qual è il modo più rapido per farlo?Come elencare tutte le tabelle utente in Sybase insieme al loro conteggio delle righe?

So in Oracle, si può fare qualcosa come qui di seguito, ma non sono sicuro di 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; 

risposta

18

Ecco lo SQL Sybase che fa quanto sopra:

select ob.name,st.rowcnt 
from sysobjects ob, systabstats st 
where ob.type="U" 
and st.id=ob.id 
order by ob.name 
+1

sto ottenendo più voci per molti tabelle eseguendo questa query. Penso che potrebbe essere perché ci sono più partizioni. Può essere la causa? – partha

+0

Possibilmente. Guarda la tabella sysobjects per maggiori dettagli. – Neerav

+2

gruppo Aggiunto di clausola di gestire più voci: selezionare ob.name, sum (st.rowcnt) da sysobjects ob, systabstats st dove ob.type = "U" e st.id = ob.id gruppo da nome oggetto ordine secondo il nome – Laura

0

utilizzare la query di seguito

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

Dipende dal significato del prodotto Sybase. Nel mio SQL Anywhere (9 e 11), la soluzione non funziona, ma funziona:

select table_name, count 
from systable 
where primary_root<>0 and creator=1 
order by 1 
+0

Sfortunatamente, systable è deprecato –

+0

Devo usare una versione precedente di sybase e questo ha funzionato perfettamente! –

+0

Questo ha visualizzato solo le tabelle per me che hanno DBA come proprietario. – Roger

0

selezionare ob.name, st.rowcnt da sysobjects ob, systabstats st dove b.type = 'U' e st.id = ob.id e indid = 0 ORDER bY ob.name

+0

Qui ho anche "Table 'systabstats' non trovato" – Roger

3

Se l'utente corrente è il creatore:

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

NOTA: I test in questo Sybase ASA 9.

+0

Allo stesso modo, questo ha mostrato solo tabelle che hanno DBA come proprietario. – Roger

2

Come ci possono essere più voci in systabstats tabella, la query deve essere:

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

Ho ricevuto l'errore, "Tabella 'systabstats' non trovata" – Roger

0

Questo funziona per me utilizzando "SQL centrale" con SQL Anywhere 17:

SELECT table_name, st.count 
FROM systable st 
WHERE table_type = 'BASE' 
Problemi correlati