0% found this document useful (0 votes)
29 views

Using Explicit Cursor

The document discusses the use of explicit cursors in Oracle databases. It shows examples of declaring a cursor, opening and fetching data from the cursor using different techniques like a basic fetch, a loop with fetch, and a cursor for loop. It also demonstrates using the %ROWCOUNT and %NOTFOUND attributes to check for end of data or if more than a specified number of rows were returned. The examples output employee IDs and names from an employees table where the department ID is equal to 30.

Uploaded by

dweet33k4
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Using Explicit Cursor

The document discusses the use of explicit cursors in Oracle databases. It shows examples of declaring a cursor, opening and fetching data from the cursor using different techniques like a basic fetch, a loop with fetch, and a cursor for loop. It also demonstrates using the %ROWCOUNT and %NOTFOUND attributes to check for end of data or if more than a specified number of rows were returned. The examples output employee IDs and names from an employees table where the department ID is equal to 30.

Uploaded by

dweet33k4
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Basis Data Lanjut

Using Explicit Cursor Katika Dwi Hapsari

Slide 10
Fetching Data from the Cursor SET SERVEROUTPUT ON DECLARE CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees WHERE department_id =30; empno employees.employee_id%TYPE; lname employees.last_name%TYPE; BEGIN OPEN emp_cursor; FETCH emp_cursor INTO empno, lname; DBMS_OUTPUT.PUT_LINE( empno ||' '||lname); END; Hasilnya :

Revisi : DECLARE CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees WHERE department_id =30; empno employees.employee_id%TYPE; lname employees.last_name%TYPE; BEGIN OPEN emp_cursor; FETCH emp_cursor INTO empno, lname; DBMS_OUTPUT.PUT_LINE( empno ||' '||lname); END;
Kartika Dwi Hapsari | 105060809111003

Basis Data Lanjut


Using Explicit Cursor Katika Dwi Hapsari

Hasilnya :

Slide 12
Fetching Data from the Cursor SET SERVEROUTPUT ON DECLARE CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees WHERE department_id =30; empno employees.employee_id%TYPE; lname employees.last_name%TYPE; BEGIN OPEN emp_cursor; LOOP FETCH emp_cursor INTO empno, lname; EXIT WHEN emp_cursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE( empno ||' '||lname); END LOOP; END;

Kartika Dwi Hapsari | 105060809111003

Basis Data Lanjut


Using Explicit Cursor Katika Dwi Hapsari

Hasilnya :

Revisi : DECLARE CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees WHERE department_id =30; empno employees.employee_id%TYPE; lname employees.last_name%TYPE; BEGIN OPEN emp_cursor; LOOP FETCH emp_cursor INTO empno, lname; EXIT WHEN emp_cursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE( empno ||' '||lname); END LOOP; END; Hasilnya :

Kartika Dwi Hapsari | 105060809111003

Basis Data Lanjut


Using Explicit Cursor Katika Dwi Hapsari

Slide 16
Cursor FOR Loops SET SERVEROUTPUT ON DECLARE CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees WHERE department_id =30; BEGIN FOR emp_record IN emp_cursor LOOP DBMS_OUTPUT.PUT_LINE( emp_record.employee_id ||' ' ||emp_record.last_name); END LOOP; END; Hasilnya :

Revisi : DECLARE CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees WHERE department_id =30; BEGIN FOR emp_record IN emp_cursor LOOP DBMS_OUTPUT.PUT_LINE( emp_record.employee_id ||' ' ||emp_record.last_name); END LOOP; END;

Kartika Dwi Hapsari | 105060809111003

Basis Data Lanjut


Using Explicit Cursor Katika Dwi Hapsari

Hasilnya :

Slide 19
%ROWCOUNT and %NOTFOUND: Example SET SERVEROUTPUT ON DECLARE empno employees.employee_id%TYPE; ename employees.last_name%TYPE; CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees; BEGIN OPEN emp_cursor; LOOP FETCH emp_cursor INTO empno, ename; EXIT WHEN emp_cursor%ROWCOUNT > 10 OR emp_cursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE(TO_CHAR(empno) ||' '|| ename); END LOOP; CLOSE emp_cursor; END ;

Kartika Dwi Hapsari | 105060809111003

Basis Data Lanjut


Using Explicit Cursor Katika Dwi Hapsari

Hasilnya :

Revisi : DECLARE empno employees.employee_id%TYPE; ename employees.last_name%TYPE; CURSOR emp_cursor IS SELECT employee_id, last_name FROM employees; BEGIN OPEN emp_cursor; LOOP FETCH emp_cursor INTO empno, ename; EXIT WHEN emp_cursor%ROWCOUNT > 10 OR emp_cursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE(TO_CHAR(empno) ||' '|| ename); END LOOP; CLOSE emp_cursor; END ;

Kartika Dwi Hapsari | 105060809111003

Basis Data Lanjut


Using Explicit Cursor Katika Dwi Hapsari

Hasilnya :

Kartika Dwi Hapsari | 105060809111003

You might also like