Avatar of Kamal Agnihotri
Kamal Agnihotri
Flag for United States of America asked on

Add sequence number to a query.

Experts,

There are 203 Tables in my Schema. The code below gives me the count of number of records in each table.

declare
     v_rowcnt integer;
 begin
     for t in (select table_name from user_tables order by 1) loop
         execute immediate 'select count(*) from ' || t.table_name into v_rowcnt;
         dbms_output.put_line(rpad(t.table_name,40,'.') || v_rowcnt);
     end loop;
 end;
/

--############################################

The first few lines of the output look like this:

ACCOUNT.................................1
AD......................................7
AD_AGE_STATED...........................6
AD_BODY.................................7
AD_CATEGORY.............................1
AD_LOCATION_STATED......................7
AD_LOCATION_WEBSITE.....................7
AD_NAME_ADVERTISED......................1

--#####################

I would like the output to look below.  Add another column to the right (First Column)  to tell the sequence number of the table. How do I modify the script above to get the desired result.

1     ACCOUNT.................................1
2     AD......................................7
3     AD_AGE_STATED...........................6
4     AD_BODY.................................7
5     AD_CATEGORY.............................1
6     AD_LOCATION_STATED......................7
7     AD_LOCATION_WEBSITE.....................7
8     AD_NAME_ADVERTISED......................1
Oracle Database

Avatar of undefined
Last Comment
slightwv (䄆 Netminder)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
slightwv (䄆 Netminder)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kamal Agnihotri

ASKER
Hi Slightwv,

With your guidance, the code below works. Would you written it any differently.
--#######################################
declare
     v_row  integer := 0;
     v_rowcnt integer;
 begin
     for t in (select table_name from user_tables order by 1) loop
         v_row := v_row + 1;
         execute immediate 'select count(*) from ' || t.table_name into v_rowcnt;
         dbms_output.put_line (rpad(v_row, 5, ' ') || (rpad(t.table_name,40,'.') || v_rowcnt));
     end loop;    
 end;    
/
slightwv (䄆 Netminder)

>>Would you written it any differently.

Yes.

I would use the XML trick in the thread I posted.

That is sort of why I posted it........

For ALL tables, remove my where clause.  I just didn't want to return them all for this small test.
drop table tab1 purge;
drop table tab2 purge;
create table tab1(col1 char(1));
create table tab2(col1 char(1));
insert into tab1 values('a');
insert into tab2 values('a');
insert into tab2 values('a');
commit;


SELECT row_number() over(order by table_name) rn, table_name,
       TO_NUMBER(EXTRACTVALUE(
                     xmltype(DBMS_XMLGEN.getxml('select count(*) X from ' || table_name)),
                     '/ROWSET/ROW/X'
                 ))
           COUNT
FROM user_tables
where table_name like 'TAB%';

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck