我在oracle中创建一个存储过程来查找所有表的行数。我必须每天运行这些程序来查找ETL之后加载的每日数据。我创建了以下程序。
create or replace Procedure Proc_RSE_TABLE_ROW_Count AS
TYPE c_table_list IS REF CURSOR ;
C_table_name c_table_list;
RSE_ROW_COUNT NUMBER;
SQL_11g VARCHAR2(500);
SQL_CURSOR VARCHAR2(500);
V_TABLE_NAME VARCHAR2(30);
BEGIN
SQL_CURSOR := 'SELECT
TRIM(table_name) table_name
FROM RSE_TABLE_COUNT ';
OPEN C_table_name FOR SQL_CURSOR;
LOOP
FETCH C_table_name INTO V_TABLE_NAME;
EXIT WHEN C_table_name%NOTFOUND;
SQL_11g := 'SELECT COUNT(1) FROM '||V_TABLE_NAME;
EXECUTE IMMEDIATE SQL_11g INTO RSE_ROW_COUNT;
INSERT into RSE_TABLE_COUNT (TABLE_NAME, ROW_COUNT, DATE_LAST_UPDATED)
values ('vtable_name', rse_row_count, sysdate)
/*UPDATE RSE_TABLE_COUNT SET ROW_COUNT=RSE_ROW_COUNT,DATE_LAST_UPDATED=sysdate
WHERE table_name=V_TABLE_NAME;
commit; */
END LOOP;
CLOSE C_table_name;
END;
但是我想要插入每日数据,而不是我目前使用的。随着更新,历史越来越多。随着插入我得到错误。我想在运行过程时在表中插入table_name,row_count和sysdate,而不会影响以前的数据。我是oracle语法的新手,所以请帮助..
答案 0 :(得分:2)
请参阅以下代码段,与您拥有的代码片段完全相似,并正确插入行。
CREATE TABLE RSE_TABLE_COUNT
(
TABLE_NAME VARCHAR2(500 BYTE),
ROW_COUNT NUMBER,
DATE_LAST_UPDATED DATE
);
CREATE OR REPLACE PROCEDURE Proc_RSE_TABLE_ROW_Count
AS
TYPE c_table_list IS REF CURSOR;
C_table_name c_table_list;
RSE_ROW_COUNT NUMBER;
SQL_11g VARCHAR2 (500);
SQL_CURSOR VARCHAR2 (500);
V_TABLE_NAME VARCHAR2 (30);
BEGIN
SQL_CURSOR := 'SELECT
object_name
FROM user_objects
WHERE object_type=''TABLE''';
OPEN C_table_name FOR SQL_CURSOR;
LOOP
FETCH C_table_name INTO V_TABLE_NAME;
EXIT WHEN C_table_name%NOTFOUND;
SQL_11g := 'SELECT COUNT(1) FROM ' || V_TABLE_NAME;
EXECUTE IMMEDIATE SQL_11g INTO RSE_ROW_COUNT;
INSERT INTO RSE_TABLE_COUNT (TABLE_NAME, ROW_COUNT, DATE_LAST_UPDATED)
VALUES (V_TABLE_NAME, rse_row_count, SYSDATE);
END LOOP;
CLOSE C_table_name;
END;
答案 1 :(得分:1)
为什么不分析架构(estimate_percent = 100)并从user_tables中选择num_rows? Probaly已经在你的ETL中有一个gather_schema_stats。之后:
insert into rse_table_count
select table_name,num_rows,sysdate
from user_tables;