Cursor for loop
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>
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>
SQL> set serverout on
SQL>
SQL> declare
2 begin
3 for my_dept_rec in (select department_id, department_name
4 from departments
5 order by 1)
6 loop
7 dbms_output.put('Department #' || my_dept_rec.department_id);
8 dbms_output.put_line(' is named ' || my_dept_rec.department_name);
9 end loop;
10 end;
11 /
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>
Related examples in the same category