Pls-00364:循环索引变量'j'使用无效

时间:2015-12-30 13:44:33

标签: sql oracle plsql

我正在尝试计算模式中的每个表。

SET serveroutput ON;
BEGIN
FOR I IN (SELECT TABLE_NAME FROM USER_TABLES) LOOP
FOR J IN (SELECT COUNT(1) CNT FROM I.TABLE_NAME) LOOP
DBMS_OUTPUT.PUT_LINE(I.TABLE_NAME||'>>'||J.CNT);
END LOOP;
END LOOP;
END;
/

但是我得到这样的错误。,

Error report:
ORA-06550: line 3, COLUMN 37:
PL/SQL: ORA-00942: TABLE OR VIEW does NOT exist
ORA-06550: line 3, COLUMN 10:
PL/SQL: SQL Statement ignored
ORA-06550: line 4, COLUMN 42:
PLS-00364: loop INDEX variable 'J' USE IS invalid
ORA-06550: line 4, COLUMN 1:
PL/SQL: Statement ignored

可以在sql查询中实现,。? /

1 个答案:

答案 0 :(得分:4)

您需要使用动态SQL来实现此目的:

DECLARE 
  val NUMBER;
BEGIN
FOR I IN (SELECT TABLE_NAME FROM USER_TABLES) LOOP
  EXECUTE IMMEDIATE 'SELECT count(*) FROM ' || i.table_name INTO val;
  DBMS_OUTPUT.PUT_LINE(i.table_name || ' ==> ' || val );
END LOOP;
END;
/