27th Example - Explicit Cursor on PL/SQL

Cursor is part of a block of PL/SQL that references to one or several tables in the database. In the PL/SQL there are two types of cursors that can be made which are implicit cursor and explicit cursor. Explicit cursor is a cursor that was made in the declaration block of PL/SQL while the implicit cursor is made ​​on the part of the content (body) PL/SQL block. Excess of the explicit cursor is able to record more than one operation while the implicit cursor can only perform one operation at a time record. PL/SQL block use explicit cursor join with looping block.
Here is an example of PL/SQL block with explicit cursor:

DECLARE
CURSOR C_Mahasiswa IS
SELECT nim, nama FROM TMahasiswa;
Var1 TMahasiswa.NIM%TYPE;
Var2 TMahasiswa.NAMA%TYPE;
BEGIN
FOR i IN C_Mahasiswa LOOP
Var1 := i.NIM;
Var2 := i.NAMA;
DBMS_OUTPUT.PUT_LINE(Var1||‘ ‘||Var2);
END LOOP;
END;
/


Above anonymous block of PL/SQL has a cursor with the name c_mahasiswa. C_mahasiswa cursor will accommodate nim and nama attributes of the table tmahasiswa. By using the FOR..LOOP looping, PL/SQL block able to display all the data nim and nama that is on the table tmahasiswa (dbms_output.put_line (var1 | | '' | | var2)).

No comments

Powered by Blogger.