我有以下脚本来计算特定所有者的每个表的所有行。它完美地运作。 但是,有些表有一个名为'OLD'的特定列,而其他表没有...... 如果此列存在与否,我当前的脚本不会考虑:
DECLARE
val NUMBER;
BEGIN
FOR I IN (SELECT table_name FROM all_tables where owner='myowner') LOOP
EXECUTE IMMEDIATE 'SELECT count(*) FROM myowner.' || i.table_name INTO val;
DBMS_OUTPUT.PUT_LINE(i.table_name || ';' || val );
END LOOP;
END;
所以我想补充的是: 如果OLD列存在,则在OLD = 0(其中OLD = 0)时将其考虑在内,如果它不存在则继续执行“正常”计数而不考虑此列。 希望我已经足够清楚了;)
非常感谢!
一个例子: 假设我有两张桌子:
Table1 - columns A B C with the following data:
1 "test" "Steve"
2 "test2" "George"
Table2 - columns E F G OLD with the following data:
1 "test3" "Martin" 0
2 "test4" "Lucas" 0
3 "test5" "Marley" 0
4 "test6" "Bob" 55
结果应该是:
表1; 2 - >没有'OLD'栏,所以我做了一个简单的计数 返回2
表2; 3 - >有一个'OLD'列,所以我做了一个OLD = 0的计数 它然后返回3
答案 0 :(得分:0)
尝试:
DECLARE
val NUMBER;
BEGIN
FOR I IN (
select a.table_name, c.column_name
from all_tables a
left join all_tab_cols c
ON a.owner = c.owner and a.table_name = c.table_name and c.column_name = 'OLD'
where a.owner = 'MYOWNER'
)
LOOP
IF i.column_name IS NULL THEN
EXECUTE IMMEDIATE 'SELECT count(*) FROM MYOWNER.' || i.table_name INTO val;
ELSE
EXECUTE IMMEDIATE 'SELECT count(*) FROM MYOWNER.' || i.table_name
|| ' WHERE old = 0' INTO val;
END IF;
DBMS_OUTPUT.PUT_LINE(i.table_name || ';' || val );
END LOOP;
END;