26th Example - Cursor Based Record on PL/SQL

In the previous module has been described on table-based record. At this time will be explained about the cursor-based record. In contrast to table-based record, cursor-based record has characteristics not directly interact with the table, but interact through the mediation CURSOR. Cursor is a declaration on the block PL/SQL to create a variable that contains a query from one or more existing tables in the database. Here is an example of using cursor-based record (use tmahasiwa table that's been made before):

DECLARE
   CURSOR c_mhs IS SELECT nim, nama FROM mahasiswa;
   mhs_rec c_mhs%ROWTYPE;
BEGIN
   OPEN c_mhs;
   LOOP
     FETCH c_mhs INTO mhs_rec;
     EXIT WHEN c_mhs%NOTFOUND;
     DBMS_OUTPUT.PUT_LINE(mhs_rec.nim||' '||mhs_rec.nama);
   END LOOP;
   CLOSE c_mhs;
END;
/


PL/SQL block above has a cursor with the name c_mhs. Cursor c_mhs contains a query that took nim and nama attributes of the table tmahasiswa. Mhs_rec variable has a data type c_mhs% ROWTYPE, which means it is able to accommodate variable nim and nama of the cursor c_mhs. On the contents of block PL/SQL cursor variable c_mhs loaded in mhs_rec inside a loop block. Cursor will continue to be read until the end and the result is displayed to the screen by calling mhs_rec.nim and mhs_rec.nama.

No comments

Powered by Blogger.