Mega Code Archive

 
Categories / Oracle PLSQL / Cursor
 

Use for loop cursor

SQL> SQL> CREATE TABLE departments   2  (department_id           number(10)            not null,   3   department_name      varchar2(50)      not null,   4   CONSTRAINT departments_pk PRIMARY KEY (department_id)   5  ); Table created. SQL> SQL> insert into departments ( department_id, department_name )   2                    values( 1,             'Data Group' ); 1 row created. SQL> SQL> insert into departments ( department_id, department_name )   2                    values( 2,             'Purchasing' ); 1 row created. SQL> SQL> insert into departments ( department_id, department_name )   2                    values( 3,             'Call Center' ); 1 row created. SQL> SQL> insert into departments ( department_id, department_name )   2                    values( 4,             'Communication' ); 1 row created. SQL> SQL> set serveroutput on SQL> declare   2  begin   3    for my_dept_rec in (select department_id, department_name from departments order by 1)   4    loop   5      dbms_output.put('Department #' || my_dept_rec.department_id);   6      dbms_output.put_line(' is named ' || my_dept_rec.department_name);   7    end loop;   8  end;   9  / Department #1 is named Data Group Department #2 is named Purchasing Department #3 is named Call Center Department #4 is named Communication PL/SQL procedure successfully completed. SQL> SQL> DROP TABLE departments; Table dropped. SQL> --