2017-08-26 30 views
1

以下過程執行得很好,但其中的'dbms'不打印任何輸出(過程旨在記下其薪水尚未確定的emp的名稱在表中上輸入) 所述表有兩列,即1)name_of_emp 2)薪水(缺省0)dbms_output.put_line即使在'set serveroutput on'後也不打印內部過程

create or replace procedure add_sal_info 
as 
    cursor c is select * from emp_earnings; 
    cname varchar2(30); 
    csal number(5); 

begin 
    open c; 

    while(c%found) loop 
     fetch c into cname, csal; 
     if (csal = 0) then 
      dbms_output.put_line('enter salary for : ' ||' '|| cname); 
     end if; 
    end loop; 

    close c; 
end; 
/

的SERVEROUTPUT被設置爲「上」和我得到消息「過程已成功完成」執行,但它不打印沒有輸入工資的emp的名字(表中有一些名字)。 這裏有什麼癥結?

回答

0

c%found只有在取出一行之後纔是真實的,因此您永遠不會進入循環。這是一個代碼結構問題,而不是dbms_output問題。

順便說一句,這可以簡化爲:

create or replace procedure add_sal_info 
as 
    cursor c is select * from emp_earnings; 
begin 
    for r in c loop 
     if r.csal = 0 then 
      dbms_output.put_line('enter salary for : ' ||' '|| r.cname); 
     end if; 
    end loop; 
end; 

甚至

create or replace procedure add_sal_info 
as 
begin 
    for r in (
     select * from emp_earnings 
    ) 
    loop 
     if r.csal = 0 then 
      dbms_output.put_line('enter salary for : ' ||' '|| r.cname); 
     end if; 
    end loop; 
end; 

PL/SQL沒有圍繞if支架或while條件(它具有then關鍵字,而不是)。你可以把它們放在那裏,但它們被忽略。

+0

是的,明白了,那就做到了!謝謝 –

+0

很高興幫助。 (剛編輯添加一個簡化版本。) –

+0

謝謝,這個更簡單。 –

相關問題