declare
type abc IS table of varchar(2000);
def abc:= abc();
tbl_name varchar2(50);
totl_cont number;
scma_name varchar2(10) := 'SCHEMA_NAME';
begin
def.extend(3);
def(1):='table1';
def(2):='table2';
FOR i IN def.FIRST .. def.LAST
LOOP
select count(*) into totl_cont from scma_name.def(i);
END LOOP;
end;
在上面的代码scma_name.def(i)
中无效。是不是可以访问这样的表名?我的目标是从列表中提到的表中获取行计数。我无法从架构中创建所有表,因为我只想对所选表进行行计数。
然而,这个for循环有效 -
FOR i IN def.FIRST .. def.LAST
LOOP
select def(i) into tbl_name from dual;
DBMS_OUTPUT.PUT_LINE(tbl_name);
END LOOP;
我无法理解为什么在与变量中保存的模式名一起使用时,不能使用表名。
答案 0 :(得分:0)
最终工作代码:
declare
type abc IS table of varchar2(2000);
def abc:= abc();
totl_cont number;
scma_name varchar2(10) := 'schema_name';
totl_name varchar2(100);
sql_stmt varchar2(200);
begin
def.extend(n); -- where n is the number of tables you are using e.g i would be using def.extend(3)
def(1):='table1';
def(2):='table2';
def(3):='table3';
--def(n):='add as many tables you want, given they should be present in the schema :)'
FOR i IN def.FIRST .. def.LAST
LOOP
select scma_name||'.'||def(i) into totl_name from dual;
sql_stmt:= 'select count(*) from '||totl_name;
EXECUTE IMMEDIATE sql_stmt into totl_cont;
DBMS_OUTPUT.PUT_LINE(def(i) ||' -- ' ||totl_cont);
END LOOP;
end;
您可以在DBMS_OUTPUT
标签