跟随嵌套循环有什么问题,它会抛出以下错误:
ORA-00936:缺少表达
SET SERVEROUT ON;
SET FEED OFF;
DECLARE
schema_name varchar2(100);
table_name varchar2(100);
BEGIN
FOR outer_rec IN (select * from scott.table_list) LOOP
FOR inner_rec IN (select a.* from all_tab_columns a where a.column_name =outer_rec.table_name(+) and a.owner='SCOTT' order by a.table_name) LOOP
dbms_output.put_line (q'[||]' ||inner_rec.column_name || q'[||'|@@@@|']');
END LOOP;
END LOOP;
END;
/
答案 0 :(得分:1)
由于您使用两个单独的游标,因此无法将结果外部连接在一起。处理此问题的更好方法是使用单个游标执行您需要完成的操作,如:
BEGIN
FOR aRow IN (select *
FROM scott.table_list t
LEFT OUTER JOIN all_tab_columns a
ON (a.owner = 'SCOTT' AND
a.table_name = t.table_name)
order by a.table_name)
LOOP
dbms_output.put_line (q'[||]' || aRow.column_name || q'[||'|@@@@|']');
END LOOP;
END;
请注意,这也使用ANSI语法进行左外连接,这比旧的Oracle风格((+))语法更受欢迎。
分享并享受。
答案 1 :(得分:0)
我会这样写(也就是说,你的变量没有被使用):
SET SERVEROUT ON;
SET FEED OFF;
DECLARE
schema_name VARCHAR2(100);
table_name VARCHAR2(100);
CURSOR c_get_tables IS
SELECT * FROM scott.table_list;
CURSOR c_get_all(c_name_in IN scott.table_name%TYPE) IS
SELECT a.*
FROM all_tab_columns a
WHERE a.table_name = c_name_in(+)
AND a.owner = 'SCOTT'
ORDER BY a.table_name;
BEGIN
FOR outer_rec IN c_get_tables
LOOP
FOR inner_rec IN c_get_all(outer_rec.table_name)
LOOP
dbms_output.put_line(q'[||]' || inner_rec.column_name ||
q'[||'|@@@@|']');
END LOOP;
END LOOP;
END;
/
答案 2 :(得分:0)
试试这个
SET SERVEROUT ON;
SET FEED OFF;
DECLARE
schema_name varchar2(100);
table_name varchar2(100);
BEGIN
FOR outer_rec IN (select * from scott.table_list) LOOP
FOR inner_rec IN (select a.* from all_tab_columns a where a.table_name =outer_rec.table_name and a.owner='SCOTT' order by a.table_name) LOOP
dbms_output.put_line (q'[||]' ||inner_rec.column_name || q'[||'|@@@@|']');
END LOOP;
END LOOP;
END;
/