我在循环中执行了一次获取。如果此提取失败(无数据),我希望CONTINUE
循环到EXCEPTION
内的下一条记录。
这可能吗?
我得到ORA-06550
& PLS-00201 identifer CONTINUE must be declared
DECLARE
v_attr char(88);
CURSOR SELECT_USERS IS
SELECT id FROM USER_TABLE
WHERE USERTYPE = 'X';
BEGIN
FOR user_rec IN SELECT_USERS LOOP
BEGIN
SELECT attr INTO v_attr
FROM ATTRIBUTE_TABLE
WHERE user_id = user_rec.id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- user does not have attribute, continue loop to next record.
CONTINUE;
END;
END LOOP;
END;
答案 0 :(得分:13)
CONTINUE
语句是11g中的新功能。
答案 1 :(得分:10)
在您提供的构造中,您不需要CONTINUE。一旦处理了异常,就会执行END之后的语句,假设您的EXCEPTION块没有终止该过程。换句话说,它将继续进行user_rec循环的下一次迭代。
您还需要在BEGIN块内选择一个变量:
SELECT attr INTO v_attr FROM attribute_table...
显然你必须声明v_attr ......
答案 2 :(得分:6)
关于ole goto声明怎么样(我知道,但是它知道,但它在这里工作正常;)
DECLARE
v_attr char(88);
CURSOR SELECT_USERS IS
SELECT id FROM USER_TABLE
WHERE USERTYPE = 'X';
BEGIN
FOR user_rec IN SELECT_USERS LOOP
BEGIN
SELECT attr INTO v_attr
FROM ATTRIBUTE_TABLE
WHERE user_id = user_rec.id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- user does not have attribute, continue loop to next record.
goto end_loop;
END;
<<end_loop>>
null;
END LOOP;
END;
当然,只需将end_loop放在循环的最后。 null可以用提交替代,也可以用计数器增量替换,直到你。
答案 3 :(得分:0)
对于这个例子,你真的应该只使用外连接。
declare
begin
FOR attr_rec IN (
select attr
from USER_TABLE u
left outer join attribute_table a
on ( u.USERTYPE = 'X' and a.user_id = u.id )
) LOOP
<process records>
<if primary key of attribute_table is null
then the attribute does not exist for this user.>
END LOOP;
END;
答案 4 :(得分:0)
Notice you can use WHEN exception THEN NULL
the same way as you would use WHEN exception THEN continue
. Example:
DECLARE
extension_already_exists EXCEPTION;
PRAGMA EXCEPTION_INIT(extension_already_exists, -20007);
l_hidden_col_name varchar2(32);
BEGIN
FOR t IN ( SELECT table_name, cast(extension as varchar2(200)) ext
FROM all_stat_extensions
WHERE owner='{{ prev_schema }}'
and droppable='YES'
ORDER BY 1
)
LOOP
BEGIN
l_hidden_col_name := dbms_stats.create_extended_stats('{{ schema }}', t.table_name, t.ext);
EXCEPTION
WHEN extension_already_exists THEN NULL; -- ignore exception and go to next loop iteration
END;
END LOOP;
END;