Cursor for loop : Implicit Cursor « Cursor « Oracle PL / SQL





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

1.Use implicit cursor
2.Implicit cursors: SQL%FOUND returns TRUE if SQL statement found one or more records
3.use for loop cursor
4.Implicit Cursors: sql%rowcount
5.Reference implicit in insert statement
6.use cursor attributes on the implicit SQL cursor.
7.Implicit cursor by for loop
8.Test cursor attributes with an implicit cursor
9.Use implicit or explicit cursor to insert 50000 rows to a table
10.Use passed in value with creating a cursor
11.Write an implicit cursor in a FOR loop and use the data
12.explicit cursor with cursor variable
13.for data in ( select * from tableName )