Exact fetch returns more than requested number of rows when trying to fetch multiple records from two columns

StackOverflow https://stackoverflow.com/questions/21291346

  •  01-10-2022
  •  | 
  •  

문제

I am writing a procedure to output all the table_name and num_rows in user_tables. But i am getting exact fetch returns more than requested number of rows. Any tips or suggestions are welcomed.

create or replace procedure output_table
as
v_table_name varchar2(20);
v_num_rows number;

begin
select table_name, num_rows 
into v_table_name, v_num_rows
from user_tables;

dbms_output.put_line('Table '||v_table_name||' has '||v_num_rows||' of rows ');
end;
/

execute output_table;
/
도움이 되었습니까?

해결책

You probably want to have a loop

BEGIN
  FOR t IN (SELECT table_name, num_rows
              FROM user_tables)
  LOOP
    dbms_output.put_line( 'Table ' || t.table_name || 
                          ' has ' || t.num_rows || ' rows.' );
  END LOOP;
END;

I hope that you understand that the num_rows value in user_tables may have little or no relationship to the actual number of rows in the table and that it is highly unlikely to be a currently correct value.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top