【问题标题】:dbms_output.put_line not printing inside procedure even after 'set serveroutput on'dbms_output.put_line 即使在“设置 serveroutput on”之后也不会在过程中打印
【发布时间】:2018-02-04 04:29:45
【问题描述】:

下面的程序执行得很好,但是里面的“dbms”没有打印任何输出。(程序的意思是记下工资尚未输入表中的雇员的名字) 该表有两列,即 1)name_of_emp 2)salary (默认 0)

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;
/

服务器输出设置为“开”,我在执行时收到消息“程序已成功完成”,但它不打印未输入工资的员工的姓名(表中有一些)。 这里有什么症结吗?

【问题讨论】:

    标签: sql stored-procedures plsql oracle10g plsqldeveloper


    【解决方案1】:

    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 在ifwhile 条件周围没有括号(它有then 关键字)。您可以将它们放在那里,但它们会被忽略。

    【讨论】:

    • 很高兴它有帮助。 (刚刚编辑添加了一个简化版本。)
    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多