使用Oracle编写具有结果行计数和列计数的动态存储过程

z3yyvxxp  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(118)

使用Oracle平台显示行计数和列计数。我需要写一个动态存储过程。
它应该使用Oracle平台行计数列计数显示。
查询应该是动态的,并返回特定模式的确切结果。

zrfyljdw

zrfyljdw1#

这里有一个选择;它从user_tables中选择表,使用动态SQL计算该表中的行数,并使用数据字典(user_tab_columns)选择该表中的列数:

SQL> create or replace procedure p_test is
  2    l_rows number;
  3    l_cols number;
  4  begin
  5    for cur_r in (select table_name from user_tables
  6                  where rownum <= 5) loop
  7      execute immediate 'select count(*) from ' || cur_r.table_name
  8        into l_rows;
  9      select count(*) into l_cols from user_Tab_columns where table_name = cur_r.table_name;
 10
 11      dbms_output.put_line(cur_r.table_name || ': ' || l_rows || ' rows and ' ||
 12        l_cols || ' columns');
 13    end loop;
 14  end;
 15  /

Procedure created.

SQL> exec p_test;
TEST_NEW_2: 2 rows and 3 columns
TABLE1: 1 rows and 1 columns
TEST_NEW: 2 rows and 3 columns
TABLE2: 1 rows and 1 columns
TABLE3: 1 rows and 1 columns

PL/SQL procedure successfully completed.

SQL>
zi8p0yeb

zi8p0yeb2#

为了好玩:

CREATE OR REPLACE PACKAGE count_rowcols_pkg AS 
 
  FUNCTION DESCRIBE ( 
    tab        IN OUT dbms_tf.table_t 
  ) RETURN dbms_tf.describe_t; 
 
  PROCEDURE fetch_rows; 
 
END count_rowcols_pkg;  
/

CREATE OR REPLACE PACKAGE BODY count_rowcols_pkg AS 
        
    FUNCTION DESCRIBE ( 
        tab        IN OUT dbms_tf.table_t
    ) Return dbms_tf.describe_t 
    AS 
    BEGIN  
        FOR I IN 1 .. TAB.COLUMN.COUNT LOOP 
                       
            tab.column(i).for_read := true; 
            tab.column(i).pass_through := false; 
                        
        END LOOP; 

        
        RETURN dbms_tf.describe_t(
                 new_columns => dbms_tf.columns_new_t(
                   1 => dbms_tf.column_metadata_t(NAME => 'n_rows',
                                                  TYPE => dbms_tf.type_number),
                   2 => dbms_tf.column_metadata_t(NAME => 'n_cols',
                                                  TYPE => dbms_tf.type_number)),
                    row_replication => true
                );

    END DESCRIBE;

    PROCEDURE fetch_rows  
    AS 
        env    dbms_tf.env_t := dbms_tf.get_env();
        v_rowset    dbms_tf.row_set_t;  
        v_newrowset    dbms_tf.row_set_t;  
        v_rowcount  PLS_INTEGER;  
        v_colcount  PLS_INTEGER;  
        v_repfac dbms_tf.tab_naturaln_t;
        v_rowcount_col dbms_tf.tab_number_t;
        v_colcount_col dbms_tf.tab_number_t;
    BEGIN 
        dbms_tf.get_row_set(v_rowset, v_rowcount, v_colcount);  

        FOR r IN 1..v_rowcount LOOP
            v_repfac(r) := 0 ;
        END LOOP;
        v_repfac(1) := 1 ;
        
        v_rowcount_col(1) := v_rowcount;
        v_colcount_col(1) := v_colcount ;
        
        dbms_tf.row_replication(replication_factor => v_repfac);
        dbms_tf.put_col(1,v_rowcount_col); 
        dbms_tf.put_col(2,v_colcount_col); 
    END fetch_rows;
    
END count_rowcols_pkg
;
/


CREATE OR REPLACE FUNCTION count_rowcols(tab TABLE)
                  RETURN TABLE  
PIPELINED ROW POLYMORPHIC USING count_rowcols_pkg;
/

select * from count_rowcols(DUAL) ;

1   1

select * from count_rowcols(USER_TABLES) ;

145 86

注意:如果表是空的,它不会返回任何东西...并生成异常ORA-62558,如果表中有LONG列...
但你可以这样做,你将无法与其他方法:

with data(id, pow, ch) as (
    select level, power(2,level-1), chr(32+level) from dual
    connect by level <= 26
)
select * from count_rowcols_t(data) ;

26  3

相关问题