ORACLENERD twitter/oraclenerd view chet justice's profile on LinkedIn feed oraclenerd t-shirts Unemployment Clock:

  Dynamic Single Row Query
Once in awhile doing analysis, I'd like to see the count from each table, just to get an idea of how much data I am working with. Of course this doesn't measure width, but it is a metric.

Of course I could go through and write a SELECT COUNT(*) from every table, that works if there are like 4 tables. Anything more and...well it sucks.

Yes, I could gather stats on the schema and then reference NUM_ROWS, but this is an occasion where I don't have the necessary privileges.

I've been trying to do this for years, with no luck, until yesterday. Typically I would do something like this:
DECLARE
l_count INTEGER;
l_sql VARCHAR2(200);
BEGIN
FOR i IN ( SELECT table_name
FROM dba_tables
WHERE owner = 'SYSTEM' )
LOOP
l_sql := 'SELECT COUNT(*) INTO :1 FROM SYSTEM.' || i.table_name;
EXECUTE IMMEDIATE l_sql USING l_count, i.table_name;
dbms_output.put_line( i.table_name || '-' || l_count );
END LOOP;
END;
/
Which would of course would give me this:
DECLARE
*
ERROR at line 1:
ORA-01745: invalid host/bind variable name
ORA-06512: at line 10
Only now did I realize a fatal flaw with that...I was trying to bind the variable into the string, which would obviously never work.

For whatever reason, this time I decided to research it. I came across Flavio Cassetta's site and this post, SQL Error: ORA-01745: invalid host/bind variable namI'll skip a few iterations and get to the final product (because I went through this exercise on Friday night and quite honestly, don't remember all the permutations).
SET SERVEROUTPUT ON

DECLARE
l_count INTEGER;
l_table VARCHAR2(61);
BEGIN
FOR i IN ( SELECT table_name
FROM dba_tables
WHERE owner = 'SYSTEM' )
LOOP
l_table := 'SYSTEM.' || i.table_name;
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || l_table INTO l_count;
dbms_output.put_line( l_table || '-' || l_count );
END LOOP;
END;
/

...snip
SYSTEM.LOGMNR_USER$-0
SYSTEM.LOGMNR_OBJ$-0
SYSTEM.LOGMNR_DICTIONARY$-0
SYSTEM.LOGMNR_DICTSTATE$-0
SYSTEM.OL$NODES-0
SYSTEM.OL$HINTS-0
SYSTEM.OL$-0

PL/SQL procedure successfully completed.
Done.

Labels: ,

 
Comments: Post a Comment



Links to this post:

Create a Link



<<Home


Guest Authors

How To

Popular

Previous Posts

Code Projects

Archives
August 2007 / September 2007 / October 2007 / November 2007 / December 2007 / January 2008 / February 2008 / March 2008 / April 2008 / May 2008 / June 2008 / July 2008 / August 2008 / September 2008 / October 2008 / November 2008 / December 2008 / January 2009 / February 2009 / March 2009 / April 2009 / May 2009 / June 2009 / July 2009 / August 2009 / September 2009 / October 2009 / November 2009 / December 2009 / January 2010 / February 2010 / March 2010 /


Aggregated by OraNA