Modul - ProgDB - 09 - Cursor 1 PDF
Modul - ProgDB - 09 - Cursor 1 PDF
PEMROGRAMAN
SISTEM BASIS DATA
DAN SQL
Abstract Kompetensi
Apex adalah salah satu software tool Mahasiswa dapat menjelaskan isi pokok
yang dikembangkan oleh Oracle inc. dari mata kuliah teori dan aplikasi dari
Dalam pembuatan web aplication Apex & SQL serta membuat Aplikasi
yang sudah include di dalam oracle dari tool Apex.
10Gexpress
.
Silabus :
1.Review Function & Prosedure
2.Trigger
3.Cursor
4.Import-Export APEX
RETURN V_KET;
EXCEPTION
WHEN NO_DATA_FOUND THEN
TOT:=0;
END;
Membuat Function Total Gaji Tiap Departement
RETURN TOT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
TOT:=9999;
END;
select T.*,TOTAL_SUMDEPT(t.department_id) ,
KET(t.department_id) from departments t;
1.2.Creating a Procedure
A procedure is created with the CREATE OR REPLACE PROCEDURE
statement. The simplified syntax for the CREATE OR REPLACE
PROCEDURE statement is as follows:
{IS | AS}
BEGIN
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
END procedure_name;
Where,
The optional parameter list contains name, mode and types of the
parameters. IN represents that value will be passed from outside and OUT
represents that this parameter will be used to return a value outside of the
procedure.
Example:
The following example creates a simple procedure that displays the string
'Hello World!' on the screen when executed.
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
When above code is executed using SQL prompt, it will produce the
following result:
Procedure created.
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
The above procedure named 'greetings' can be called with the EXECUTE
keyword as:
EXECUTE greetings;
Hello World
BEGIN
greetings;
END;
2. TRIGGER
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
database table. A trigger is triggered automatically when an
associated DML statement is executed.
Creating Triggers
The syntax for creating a trigger is:
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
Where,
CREATE [OR REPLACE] TRIGGER trigger_name: Creates or replaces an
existing trigger with the trigger_name.
{BEFORE | AFTER | INSTEAD OF} : This specifies when the trigger would
be executed. The INSTEAD OF clause is used for creating trigger on a
view.
{INSERT [OR] | UPDATE [OR] | DELETE}: This specifies the DML
operation.
[OF col_name]: This specifies the column name that would be updated.
[ON table_name]: This specifies the name of the table associated with the
trigger.
[REFERENCING OLD AS o NEW AS n]: This allows you to refer new and
old values for various DML statements, like INSERT, UPDATE, and
DELETE.
[FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger
would be executed for each row being affected. Otherwise the trigger will
execute just once when the SQL statement is executed, which is called a
table level trigger.
WHEN (condition): This provides a condition for rows for which the trigger
would fire. This clause is valid only for row level triggers.
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
CREATE OR REPLACE TRIGGER BI_T_EMP
BEFORE
insert on T_EMP
for each row
WHEN (new.salary >10000)
begin
:new.bonus:=:new.salary * 0.73;
:new.first_name:=UPPER(:new.first_name);
end;
insert INTO
t_emp(employee_id,first_name,last_name,salary,departm
ent_name,job_title)
sELECT
e.employee_id,e.first_name,e.last_name,e.salary,d.dep
artment_name,
j.job_title FROM employees e
join departments d on
d.department_id=e.department_id
join jobs j on j.job_id=e.job_id;
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
3.Cursors
Implicit cursors
Explicit cursors
Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL
statement is executed, when there is no explicit cursor for the statement.
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
Programmers cannot control the implicit cursors and the information in
it.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL
cursor, which always has the attributes like %FOUND, %ISOPEN,
%NOTFOUND, and %ROWCOUNT. The SQL cursor has additional
attributes, %BULK_ROWCOUNT and %BULK_EXCEPTIONS, designed for
use with the FORALL statement. The following table provides the
description of the most used attributes:
Attribute Description
Example:
We will be using the T_EMP table we had created and used in the
previous chapters.
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
The following program would update the table and increase salary of
each customer by 500 and use the SQL%ROWCOUNT attribute to
determine the number of rows affected:
DECLARE
total_rows number(2);
BEGIN
UPDATE t_emp
IF sql%notfound THEN
total_rows := sql%rowcount;
END IF;
END;
Explicit Cursors
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
Explicit cursors are programmer defined cursors for gaining more control
over the context area. An explicit cursor should be defined in the
declaration section of the PL/SQL Block. It is created on a SELECT
Statement which returns more than one row.
CURSOR c_employees is
SELECT
e.employee_id,e.first_name,e.last_name,e.salary,d
.department_name,
j.job_title FROM employees e
join departments d on
d.department_id=e.department_id
join jobs j on j.job_id=e.job_id;
CURSOR c_emp is
SELECT * FROM t_emp
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
Opening the cursor allocates memory for the cursor and makes it ready
for fetching the rows returned by the SQL statement into it. For example,
we will open above-defined cursor as follows:
OPEN c_employees;
CLOSE c_employee;
Example:
Following is a complete example to illustrate the concepts of explicit
cursors:
CREATE OR REPLACE PROCEDURE TEST
IS
c_employee_id employees.employee_id%type;
c_first_name employees.first_name%type;
c_last_name employees.last_name%type;
c_salary employees.salary%type;
c_department_name departments.department_name%type;
c_job_title jobs.job_title%type;
CURSOR c_employees is
SELECT
e.employee_id,e.first_name,e.last_name,e.salary,d.department_name,
j.job_title FROM employees e
join departments d on d.department_id=e.department_id
join jobs j on j.job_id=e.job_id;
BEGIN
OPEN c_employees;
LOOP
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
FETCH c_employees into c_employee_id, c_first_name,
c_last_name,c_salary,c_department_name,c_job_title;
EXIT WHEN c_employees%notfound;
dbms_output.put_line(c_employee_id || ' ' || c_first_name
|| ' ' || c_last_name|| ' ' ||c_department_name|| ' '
||c_job_title);
BEGIN
INSERT INTO
T_EMP(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,SALARY,DEPARTMENT_NAME,JOB_TI
TLE)
VALUES (c_employee_id, c_first_name,
c_last_name,c_salary,c_department_name,c_job_title);
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
ROLLBACK;
END;
END LOOP;
COMMIT;
CLOSE c_employees;
END TEST;
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
4. EXPORT & IMPORT
4.2. Application
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
Export Schema
Pada windows Start- Run-Comand
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
Import Schema
DAFTAR PUSTAKA
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id
1.abdkhaliq.com
2. www.tutorialspoint.com
3.www.plsqltutorial.com/
2016 Programan Sistem Database dan SQL Pusat Bahan Ajar dan eLearning
7 Abdul Khaliq Arrachman,S.Kom,M.Kom. http://www.mercubuana.ac.id