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

时间:2010-10-26 21:29:14

标签: oracle plsql procedure

我在Oracle 9i中有2个程序(A,B)。在个人中,他们都很好。但是我不能创建一个调用A的过程C,它反过来调用B.我在C调用A之前放置一个dbms_output.put_line,在A调用B之前调用B,在B之前放置一个。不知何故,只有第一个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;

2 个答案:

答案 0 :(得分:2)

很可能,游标c1为空,所以你的条件(当c1%notfound时退出)为真,循环在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;

答案 1 :(得分:0)

尝试在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;