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

output:

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

Expected output:

Department Names are :     
ADMIN
DEVELOPMENT
Abra
  • 19,142
  • 7
  • 29
  • 41
  • Your example in the question [working as expected](https://dbfiddle.uk/?rdbms=oracle_18&fiddle=95a1dde84b7be40eff911a3aba72e54e) – 0xdb Mar 25 '21 at 08:50

1 Answers1

1

This is because you have an anonymous BEGIN-END block with a semicolon and forward slash at the end. In this case the slash executes the code block once again.

Useful resources:

Alexander Gusev
  • 295
  • 1
  • 9