我已经写了以下功能:
declare
v_count DATE;
strsql VARCHAR2(200);
begin
for r in (select OBJECT_NAME from all_objects where OBJECT_TYPE='TABLE'
and OWNER='SALES'
and OBJECT_NAME LIKE 'T%'
ORDER BY OBJECT_NAME DESC)
loop
DBMS_OUTPUT.PUT_LINE(r.OBJECT_NAME);
strsql := 'select SCN_TO_TIMESTAMP(MAX(ORA_ROWSCN)) FROM : 1 INTO v_count';
execute immediate strsql USING r.OBJECT_NAME;
DBMS_OUTPUT.PUT_LINE(v_count);
end loop;
end;
不幸的是,发生了两件坏事:
显然,我不是Oracle开发人员,我只需要一个快速而又脏的函数来向我显示给定模式中所有表的最后更新时间。
谁能告诉我我做错了什么?
答案 0 :(得分:1)
您的strsql包含无法执行的错误SQL。所以,首先execute immediate
引发异常(确定你看到了它)。
不能从参数中使用表名。你应该像
strsql := 'select .... from ' || r.owner || '.' || r.object_name;
答案 1 :(得分:0)
经过一系列的工作,破坏,咨询了almigty Google,我想出了这个功能:
declare
v_count TIMESTAMP;
strsql VARCHAR2(200);
tblname VARCHAR2(32);
begin
for r in (select OBJECT_NAME from all_objects where OBJECT_TYPE='TABLE'
and OWNER='SALES'
and OBJECT_NAME LIKE 'T%'
ORDER BY OBJECT_NAME)
loop
begin
tblname := R.OBJECT_NAME;
strsql := 'select SCN_TO_TIMESTAMP(MAX(ORA_ROWSCN)) FROM SALES.' || tblname;
execute immediate strsql into v_count;
DBMS_OUTPUT.PUT_LINE(tblname || ' ' || v_count);
EXCEPTION
when others then
null;
end;
end loop;
end;
ORA_ROWSCN保留的时间有限,因此如果不存在,则会抛出异常。如果我“正确地”写了这个,它只会为ORA-006550返回一个null,我想找出一种方法来按SCN对结果进行排序(但是下降)。
答案 2 :(得分:0)
请将v_count的日期类型更改为timestamp,并将strsql中的select子句更改为以下内容。
SQL> declare
2 v_count timestamp;
3 strsql VARCHAR2(200);
4 begin
5
6 for r in (select OBJECT_NAME from all_objects
7 where OBJECT_TYPE='TABLE'
8 and OWNER='SCOTT'
9 and OBJECT_NAME like'T%'
10 )
11 loop
12 DBMS_OUTPUT.PUT_LINE(r.OBJECT_NAME);
13 strsql := 'select SCN_TO_TIMESTAMP(MAX(ORA_ROWSCN)) FROM '||r.OBJECT_NAME || ' ';
14 execute immediate strsql into v_count;
15 DBMS_OUTPUT.PUT_LINE(v_count);
16 end loop;
17
18 end;
19 /
TESTEMP
21-APR-15 11.52.20.000000 AM
TESTEMP2
20-APR-15 09.53.12.000000 PM
TEST_AUDIT
21-APR-15 11.52.20.000000 AM
PL/SQL procedure successfully completed.