如何从all_views获取各个视图的总记录数。 请参考以下代码以供参考..
Declare
view_name VARCHAR2(200);
v_str VARCHAR2 (1000);
v_output VARCHAR2(4000);
CURSOR tbl IS
SELECT view_name
FROM all_views
WHERE OWNER = SYS_CONTEXT( 'USERENV', 'CURRENT_SCHEMA')
ORDER BY 1 ;
BEGIN
OPEN tbl ;
LOOP
FETCH tbl INTO view_name;
EXIT WHEN tbl%NOTFOUND;
v_str := 'Select '''|| view_name ||' '' || count (*) from ' || view_name ;
EXECUTE IMMEDIATE v_str INTO v_output;
DBMS_OUTPUT.PUT_LINE(v_output);
END LOOP;
CLOSE tbl;
END;
当前输出:
V_DSP_BUSINESS_DATE 10
V_DSP_DEPARTMENT 20
V_DSP_EMPLOYEE_DEACTIVATED 50
V_DSP_EMPLOYEE_GED 80
预期输出:
sum up the record count of all individual views
i.e 160
请帮助。
答案 0 :(得分:0)
只需修改LOOP
,即可在每次获取COUNT
时进行总结。
LOOP
FETCH tbl INTO view_name;
EXIT WHEN tbl%NOTFOUND;
v_str := 'Select count (*) from ' || view_name ;
EXECUTE IMMEDIATE v_str INTO v_cnt;
v_cnt_tot := v_cnt_tot + v_cnt;
END LOOP;
DBMS_OUTPUT.PUT_LINE(v_cnt_tot);
请务必声明v_cnt
和v_cnt_tot
变量。
如果您真的想在LOOP
即row-by-row
中进行操作,请使用简单的 CURSOR FOR LOOP ,而不是声明CURSOR
。
类似的东西,
FOR i IN SELECT view_name
FROM all_views
WHERE OWNER = SYS_CONTEXT( 'USERENV', 'CURRENT_SCHEMA')
LOOP
...
答案 1 :(得分:0)
非常感谢你的帮助...... :) 它奏效了。
请在下面找到最终代码
Declare
view_name VARCHAR2(200);
v_str VARCHAR2 (1000);
v_cnt VARCHAR2(4000);
v_cnt_tot VARCHAR2(4000);
CURSOR tbl IS
SELECT view_name
FROM all_views
WHERE OWNER = SYS_CONTEXT( 'USERENV', 'CURRENT_SCHEMA')
ORDER BY 1 ;
BEGIN
v_cnt_tot := 0;
OPEN tbl ;
LOOP
FETCH tbl INTO view_name;
EXIT WHEN tbl%NOTFOUND;
v_str := 'Select count (*) as count from ' || view_name ;
EXECUTE IMMEDIATE v_str INTO v_cnt;
v_cnt_tot := v_cnt_tot + v_cnt;
END LOOP;
DBMS_OUTPUT.PUT_LINE(v_cnt_tot);
CLOSE tbl;
END;