为什么在 plsql 中执行循环时输出打印两次?

Why the output is printing twice while executing loop in plsql?

set serveroutput on;
Declare
    d_name varchar(25);
    cursor dep_cur is
    select distinct department_name from department;
BEGIN 
    dbms_output.put_line('Department Names are :');
    open dep_cur;
    
    LOOP
        fetch dep_cur
            into d_name;
              
        exit when dep_cur%notfound;    
        dbms_output.put_line(d_name);  
    END LOOP;
    close dep_cur;
END;
/

输出:

Department Names are :                                                          
ADMIN                                                                           
TESTING                                                                         
DEVELOPMENT                                                                     
Department Names are :                                                          
ADMIN                                                                           
TESTING                                                                         
DEVELOPMENT                                                                     

预期输出:

Department Names are :     
ADMIN
DEVELOPMENT

这是因为您有一个匿名 BEGIN-END 块,末尾有分号和正斜杠。在这种情况下,斜杠再次执行代码块。

有用的资源:

  • Oracle documentation
  • 中详细了解斜线的用途
  • 阅读 this thread
  • 中有关何时使用分号或斜杠的更多信息