为什么 Oracle 中一个过程不能调用另一个过程

发布于 2024-09-29 01:27:14 字数 725 浏览 4 评论 0原文

我在 Oracle 9i 中有 2 个过程(A,B)。就个人而言,它们都工作得很好。但我无法创建一个调用 A 的过程 C,A 又调用 B。我在 C 调用 A 之前、A 调用 B 之前以及 B 中的一个之前放置了 dbms_output.put_line 。不知何故,只有第一个 put_line 有效。这不起作用的可能原因是什么?谢谢你,

CREATE OR REPLACE PROCEDURE C (num in number)
as
begin
    for r in (select col1 from Table1)
    loop
        dbms_output.put_line ('Inside C');
        A(r.col1);
    end loop;
end;

CREATE OR REPLACE PROCEDURE A (var1 IN varchar2)
AS
    v1 varchar2;
    cursor c1(c_var in varchar2) is 
       select col1 from table2 where col2=c_var;
BEGIN
    open c1(var1);
    loop
        fetch c1 into v1;
        exit when c1%notfound; 
        dbms_output.put_line ('Inside A');
        B(v1);
    end loop;
    close c1;
END;

I have 2 procedures (A,B) in Oracle 9i. In individual, they all work fine. But I cannot make a procedure C that calls A, which in turn calls B. I put a dbms_output.put_line before C calls A, before A calls B and one in B. Somehow, only the first put_line works. What are the possible reasons why this doesn't work? Thank you,

CREATE OR REPLACE PROCEDURE C (num in number)
as
begin
    for r in (select col1 from Table1)
    loop
        dbms_output.put_line ('Inside C');
        A(r.col1);
    end loop;
end;

CREATE OR REPLACE PROCEDURE A (var1 IN varchar2)
AS
    v1 varchar2;
    cursor c1(c_var in varchar2) is 
       select col1 from table2 where col2=c_var;
BEGIN
    open c1(var1);
    loop
        fetch c1 into v1;
        exit when c1%notfound; 
        dbms_output.put_line ('Inside A');
        B(v1);
    end loop;
    close c1;
END;

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

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

发布评论

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

评论(2

烂人 2024-10-06 01:27:14

显然,游标 c1 为空,因此您的条件(当 c1%notfound 时退出)为 true,并且循环在 dbms_output 调用之前终止。

如果您想打印该行而不考虑空光标,请更改其位置,例如:

CREATE OR REPLACE PROCEDURE C (num in number)
as
begin
    dbms_output.put_line ('Inside C');
    for r in (select col1 from Table1)
    loop
        dbms_output.put_line ('Calling A');
        A(r.col1);
    end loop;
end;


CREATE OR REPLACE PROCEDURE A (var1 IN varchar2)
AS
    v1 varchar2;
    cursor c1(c_var in varchar2) is
       select col1 from table2 where col2=c_var;
BEGIN
    dbms_output.put_line ('Inside A');
    open c1(var1);
    loop
        fetch c1 into v1;
        exit when c1%notfound;
        dbms_output.put_line ('Calling B');
        B(v1);
    end loop;
    close c1;
END;

Obviusly, cursor c1 is empty, so your condition (exit when c1%notfound) is true and loop is terminated before the dbms_output call.

If you want to print the line regardless of a empty cursor, change it's position, for example:

CREATE OR REPLACE PROCEDURE C (num in number)
as
begin
    dbms_output.put_line ('Inside C');
    for r in (select col1 from Table1)
    loop
        dbms_output.put_line ('Calling A');
        A(r.col1);
    end loop;
end;


CREATE OR REPLACE PROCEDURE A (var1 IN varchar2)
AS
    v1 varchar2;
    cursor c1(c_var in varchar2) is
       select col1 from table2 where col2=c_var;
BEGIN
    dbms_output.put_line ('Inside A');
    open c1(var1);
    loop
        fetch c1 into v1;
        exit when c1%notfound;
        dbms_output.put_line ('Calling B');
        B(v1);
    end loop;
    close c1;
END;
烟花肆意 2024-10-06 01:27:14

尝试在 C 中放置一个异常处理程序来检测是否抛出异常;类似的东西

CREATE OR REPLACE PROCEDURE C (num in number) 
as 
begin 
    for r in (select col1 from Table1) 
    loop 
        dbms_output.put_line ('Inside C'); 
        A(r.col1); 
    end loop;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Exception caught in C : ' || SQLCODE || ' ' || SQLERRM);
    RAISE;
end;

Try putting an exception handler in C to detect if an exception is being thrown; something like

CREATE OR REPLACE PROCEDURE C (num in number) 
as 
begin 
    for r in (select col1 from Table1) 
    loop 
        dbms_output.put_line ('Inside C'); 
        A(r.col1); 
    end loop;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Exception caught in C : ' || SQLCODE || ' ' || SQLERRM);
    RAISE;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文