我是否必须打开游标才能使用隐式游标的 %FOUND 或 %NOTFOUND 游标属性?

发布于 2024-12-14 03:22:44 字数 334 浏览 0 评论 0原文

是否可以在以下 FOR 循环之后执行 c_emp%notfound 还是我必须先打开游标?

declare
 cursor c_emp is select * from employee;
 begin
    for c_rec in c_emp
    loop
        dbms_output.put_line(r_emp.first_Name);
    end loop;
 end;
 /

我想在 FOR 循环之后执行一个 UPDATE 语句,但前提是 FOR 循环确实处理了游标上的任何行。我知道我可以设置一个标志,但是有更干净的方法吗?

Is it possible to execute c_emp%notfound after the following FOR loop or do I have to open the cursor first?

declare
 cursor c_emp is select * from employee;
 begin
    for c_rec in c_emp
    loop
        dbms_output.put_line(r_emp.first_Name);
    end loop;
 end;
 /

I want to execute a statement a single UPDATE statement after the FOR loop but only if the FOR loop did process any of the rows on the cursor. I know i can set a flag but is there a cleaner way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-12-21 03:22:44

“是否可以在以下FOR之后执行c_emp%notfound
循环”

不,这会抛出ORA-01001:无效游标。游标属性仅在游标打开时才具有作用域,在此语法中位于FOREND LOOP

这是 PL/SQL 的一个丑陋的方面,但恐怕您会陷入计数困境。

"Is it possible to execute c_emp%notfound after the following FOR
loop"

No, that's going to hurl ORA-01001: invalid cursor. The cursor attributes only have scope while the cursor is open, which in this syntax is between the FOR and the END LOOP.

This is an ugly aspect of PL/SQL but I'm afraid you're stuck with a count.

懵少女 2024-12-21 03:22:44

为什么不只是:

declare
 cursor c_emp is select * from employee;

 begin

    for c_rec in c_emp
    loop
       dbms_output.put_line(r_emp.first_Name);
    end loop;

    UPDATE some_table
       SET some_values
     WHERE some_conditions
       AND EXISTS (SELECT * FROM employee WHERE r.id IS NOT NULL)
 end;
 /

我在这里假设您的员工表有一个名为 ID 的主键,您可以根据需要进行调整。

Why not just:

declare
 cursor c_emp is select * from employee;

 begin

    for c_rec in c_emp
    loop
       dbms_output.put_line(r_emp.first_Name);
    end loop;

    UPDATE some_table
       SET some_values
     WHERE some_conditions
       AND EXISTS (SELECT * FROM employee WHERE r.id IS NOT NULL)
 end;
 /

I assume here the your employee table has a primary key called ID, you can adjust as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文