我在本论坛和其他论坛上一直在阅读有关此主题的绝大多数主题,但我仍无法找到问题所在。这就是我所拥有的:
drop type RecordCount_tab;
drop type RecordCount_obj;
create type RecordCount_obj is object(Symbol varchar2(6), TimeFrame char(3), TotalCount number, minDate date, maxDate date);
/
show errors;
create type RecordCount_tab is table of RecordCount_obj;
/
show errors;
create or replace function RecordCount return RecordCount_tab is
l_RecordCount_tab RecordCount_tab := RecordCount_tab();
tName varchar2(64); vMinDate date; vMaxDate date; vCount number;
n integer :=0;
BEGIN
for r in (select table_name, substr(table_name,1,instr(table_name,'_')-1) as "BaseTable", substr(table_name,instr(table_name,'_')+1,length(table_name)-instr(table_name,'_')) as "TimeFrame" from user_tables) loop
dbms_output.put_line('select min(NewDateTime), max(NewDateTime), count(*) from '||r.table_name);
execute immediate 'select min(NewDateTime), max(NewDateTime), count(*) from '||r.table_name into vMinDate, vMaxDate, vCount;
n:=n+1;
l_RecordCount_tab(n) := (r.BaseTable, r.TimeFrame, vCount, vMinDate, vMaxDate);
end loop;
return l_RecordCount_tab;
END;
/
show errors;
我一直在
PLS-00302:组件' BASETABLE'必须声明
并且我不明白为什么会这样,因为查询从sqlplus运行得非常好。我也试图删除别名,但没有用。
我错过了什么?
答案 0 :(得分:1)
您应该删除for
:
( select table_name
, substr(table_name,1,instr(table_name,'_')-1) BaseTable
, substr(table_name,instr(table_name,'_')+1,length(table_name)-instr(table_name,'_')) TimeFrame from user_tables
)
作为一个简单的例子。这有效:
begin
for r in (select 'x' d from dual) -- NO quotes
loop
dbms_output.put_line(r.d); -- NO quotes
end loop;
end;
虽然这不是:
begin
for r in (select 'x' "d" from dual) -- double quotes
loop
dbms_output.put_line(r.d); -- NO quotes
end loop;
end;
但这样做:
begin
for r in (select 'x' "d" from dual) -- double quotes
loop
dbms_output.put_line(r."d"); -- double quotes
end loop;
end;