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
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
2条答案
按热度按时间zrfyljdw1#
这里有一个选择;它从
user_tables
中选择表,使用动态SQL计算该表中的行数,并使用数据字典(user_tab_columns
)选择该表中的列数:zi8p0yeb2#
为了好玩:
注意:如果表是空的,它不会返回任何东西...并生成异常ORA-62558,如果表中有LONG列...
但你可以这样做,你将无法与其他方法: