SQL - Cursor if no records not working
CREATE OR REPLACE procedure verify_data
IS
cursor c1 is
select
e.name
from
table3 e
where
id IN (select id from table1)
and id in (select id from table2);
BEGIN
if c1%notfound then
DBMS_OUTPUT.PUT_LINE('no records found');
end if;
FOR eData in c1
LOOP
DBMS_OUTPUT.PUT_LINE(eData.name);
END LOOP;
END;
/
如果有记录,我的结果将被输出。但是如果没有找到记录,则没有显示任何内容..是否有任何异常处理或我可以做的事情,如果在select语句中没有记录,我可以显示我的输出“No records found”。
更新
我在
后添加了END LOOP
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('no records found');
和END之前;
SQL>执行verify_data();
PL / SQL程序已成功完成。
不显示..
答案 0 :(得分:0)
结束前;添加以下代码
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('no records found');
答案 1 :(得分:0)
请查看以下程序
CREATE OR REPLACE procedure verify_data
IS
cursor c1 is
select
e.name
from
table3 e
where
id IN (select id from table1)
and id in (select id from table2);
BEGIN
loop
FETCH c1 INTO eData;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(eData.name);
end loop;
exception
when no_data_found then
dbms_output.put_line('*** Exc: no data');
END;
/
答案 2 :(得分:0)
试试这段代码:
CREATE OR REPLACE procedure verify_data
IS
no_result exception;
cursor c1 is
select
e.name
from
table3 e
where
id IN (select id from table1)
and id in (select id from table2);
BEGIN
FOR eData in c1
LOOP
DBMS_OUTPUT.PUT_LINE(eData.name);
if c1%notfound then
raise no_result;
end if;
END LOOP;
EXCEPTION
WHEN no_result THEN
DBMS_OUTPUT.PUT_LINE('no records found');
END;
/
答案 3 :(得分:0)
您无法处理游标/循环
中的NO_DATA_FOUND尝试以下示例
CREATE OR REPLACE PROCEDURE verify_data
IS
CURSOR c1 is
SELECT
e.name
FROM
table3 e
WHERE
id IN (SELECT ID FROM table1)
AND id in (SELECT ID FROM table2);
eData table3.name%TYPE ;
no_data EXCEPTION ;
BEGIN
OPEN c1 ;
LOOP
FETCH c1 INTO eData;
IF c1%NOTFOUND THEN
RAISE no_data ;
ELSE
DBMS_OUTPUT.PUT_LINE(eData);
EXIT ;
END IF;
END LOOP ;
CLOSE c1;
EXCEPTION
WHEN no_data THEN
DBMS_OUTPUT.PUT_LINE('*** Exc: no data');
END;