declare
v_cnt NUMBER;
C SYS_REFCURSOR;
TMP_TBL_NM VARCHAR2(100);
stmt VARCHAR2(1000);
the_name varchar2 (50);
cursor c_table is
(SELECT a.table_name
--, a.column_name, a.constraint_name, c.owner,
--c.r_owner , c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner AND c.r_constraint_name = c_pk.constraint_name
WHERE c.owner like '%LTR' and c_pk.table_name like '%EnumerationValue%');
begin
stmt := 'SELECT table_name FROM ' || TMP_TBL_NM || ' ORDER BY 1';
OPEN C FOR stmt;
for t in C
loop
FETCH C INTO the_name;
EXIT WHEN C%NOTFOUND;
--my query for each table goes here
end loop;
end;
我想在循环中使用表名来根据我的查询检查记录。 它只是通过在循环中获取表名来给我错误。 如何在循环中获取表名,以便我可以根据我的要求从循环中的每个表中获取行。
提前谢谢。答案 0 :(得分:2)
我不完全确定你想要在这里实现什么,但如果它只是你感兴趣的表名,你可以通过这种方式使你的代码变得更简单:
declare
cursor c_table is
SELECT a.table_name
--, a.column_name, a.constraint_name, c.owner,
--c.r_owner , c_pk.table_name r_table_name, c_pk.constraint_name r_pk
FROM all_cons_columns a
JOIN all_constraints c ON a.owner = c.owner AND a.constraint_name = c.constraint_name
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner AND c.r_constraint_name = c_pk.constraint_name
begin
for t in c_table loop
--do something with the table name
dbms_output.put_line(t.table_name);
end loop;
end;
这种for循环还处理光标的打开和关闭,是进行这些操作的推荐方法。