You are on page 1of 22

06 – Pengulangan (PL-SQL)

Gunakan Database HR
Gunakan database HR
Contoh Basic Loops menambah data
Example:
DECLARE
countryid locations.country_id%TYPE := 'CA';
loc_id locations.location_id%TYPE;
counter NUMBER(2) := 1;
new_city locations.city%TYPE := 'Montreal';
BEGIN
SELECT MAX(location_id) INTO loc_id FROM locations
WHERE country_id = countryid;
LOOP
INSERT INTO locations(location_id, city, country_id)
VALUES((loc_id + counter), new_city, countryid);
counter := counter + 1;
EXIT WHEN counter > 3;
END LOOP;
END;
/
Contoh Basic Loops menambah data
select * from locations
where country_id='CA'  Melihat data locations sebelum ditambah

Example:
DECLARE
countryid locations.country_id%TYPE := 'CA';
loc_id locations.location_id%TYPE;
counter NUMBER(2) := 1;
new_city locations.city%TYPE := 'Montreal';
BEGIN
SELECT MAX(location_id) INTO loc_id FROM locations
WHERE country_id = countryid;
LOOP
INSERT INTO locations(location_id, city, country_id)
VALUES((loc_id + counter), new_city, countryid);
counter := counter + 1;
EXIT WHEN counter > 3;
END LOOP;
END;

select * from locations


 Melihat data locations setelah ditambah
where country_id='CA'
WHILE Loops
Syntax:
WHILE condition LOOP
statement1;
statement2;
. . .
END LOOP;

Use the WHILE loop to repeat statements while a


condition is TRUE.
WHILE Loops
Example:
DECLARE
countryid locations.country_id%TYPE := 'CA';
loc_id locations.location_id%TYPE;
new_city locations.city%TYPE := 'Montreal';
counter NUMBER := 1;
BEGIN
SELECT MAX(location_id) INTO loc_id FROM locations
WHERE country_id = countryid;
WHILE counter <= 3 LOOP
INSERT INTO locations(location_id, city, country_id)
VALUES((loc_id + counter), new_city, countryid);
counter := counter + 1;
END LOOP;
END;
/

Lihat lagi isi datanya :


select * from locations
where country_id='CA'
FOR Loops
• Use a FOR loop to shortcut the test for the number
of iterations.
• Do not declare the counter; it is declared
implicitly.
• 'lower_bound .. upper_bound' is required syntax.
FOR counter IN [REVERSE]
lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;
FOR Loops
Example:
DECLARE
countryid locations.country_id%TYPE := 'CA';
loc_id locations.location_id%TYPE;
new_city locations.city%TYPE := 'Montreal';
BEGIN
SELECT MAX(location_id) INTO loc_id
FROM locations
WHERE country_id = countryid;
FOR i IN 1..3 LOOP
INSERT INTO locations(location_id, city, country_id)
VALUES((loc_id + i), new_city, countryid );
END LOOP;
END;
/
FOR Loops
Guidelines
• Reference the counter within the loop only; it is
undefined outside the loop.
• Do not reference the counter as the target of an
assignment.
• Neither loop bound should be NULL.
Guidelines While Using Loops
• Use the basic loop when the statements inside the
• loop must execute at least once.
• Use the WHILE loop if the condition has to be evaluated at the
start of each iteration.
• Use a FOR loop if the number of iterations is
• known.
Nested Loops and Labels
• Nest loops to multiple levels.
• Use labels to distinguish between blocks and
loops.
• Exit the outer loop with the EXIT statement that
references the label.
Nested Loops and Labels
...
BEGIN
<<Outer_loop>>
LOOP
counter := counter+1;
EXIT WHEN counter>10;
<<Inner_loop>>
LOOP
...
EXIT Outer_loop WHEN total_done = 'YES';
-- Leave both loops
EXIT WHEN inner_done = 'YES';
-- Leave inner loop only
...
END LOOP Inner_loop;
...
END LOOP Outer_loop;
END;
/
Latihan
1. Buat sebuah tabel dengan nama REGIONS_xxx dengan struktur
yang sama dengan table REGIONS
• Melihat struktur tabel : describe regions
Jawab : membuat table REGIONS_XXX
CREATE TABLE regions_xxx
(region_id NUMBER(3) NOT NULL PRIMARY KEY,
region_name VARCHAR2(25))
Latihan
2. Gunakan perulangan (basic LOOP) untuk memasukkan data daritabel
REGIONS dengan region_id = 1, 2, 3, 4 ke dalam tabel regions_xxx !
Tabel regions :

Tabel regions_xxx :
select * from regions_xxx
Jawaban lat 2 : program mengisi data tabel regions_xxx yang sama
dengan data regions

Periksa Tabel regions_xxx :


select * from regions_xxx
Latihan 3
1. Buat sebuah tabel dengan nama JOBS_123 dengan struktur yang
sama dengan table JOBS !
2. Tambahkan field (alter table) di tabel jobs dengan nama id tipe
number(2)
3. Isi data ID dari 1 s.d 5 dengan perintah update
4. Tambahkan juga field di file jobs_123
5. Masukkan data di tabel Jobs_123 dari tabel jobs dengan perintah
BASIC LOOP
Latihan 3
• Buat sebuah tabel dengan nama JOBS_123 dengan struktur yang
sama dengan table JOBS

Jawab :
CREATE TABLE jobs_123
(job_id varchar2(10) NOT NULL PRIMARY KEY, job_title
varchar2(35) NOT NULL , min_salary number(6), max_salary
number (6))
• Tambah FIELD table JOBS
• alter table jobs add (id number(2));
• Isi data ID dari 1 s.d 5 dengan perintah update
• update jobs set id=1 where job_id='AD_PRES' Periksa Tabel jobs :
• update jobs set id=2 where job_id='AD_VP' select * from jobs
• update jobs set id=3 where job_id='AD_ASST'
• update jobs set id=4 where job_id='FI_MGR'
• update jobs set id=5 where job_id='FI_ACCOUNT‘
• Tambah FIELD table JOBS_123
• alter table jobs_123 add (id number(2));
• Tampilkan Tabel jobs_123 :
• select * from jobs_123

• ISI data di tabel JOBS_123 dengan BASIC LOOP


ISI data di tabel JOBS_123 dengan BASIC LOOP
• DECLARE
• j_id jobs.job_id%type;
• j_title jobs.job_title%type; Periksa Tabel jobs_123 :
• min_sal jobs.min_salary%type; select * from jobs_123
• max_sal jobs.max_salary%type;
• no jobs.id%type;
• counter NUMBER := 1;
• BEGIN
• LOOP
• SELECT job_id, job_title, min_salary,max_salary,id INTO
• j_id, j_title, min_sal,max_sal,no FROM jobs WHERE id = counter;
• INSERT INTO jobs_123 VALUES(j_id, j_title, min_sal,max_sal,no);
• counter := counter + 1;
• EXIT WHEN counter > 5;
• END LOOP;
• END;
Latihan 4 dan 5
4. Buat sebuah tabel dengan nama DEPARTMENTS_NIM dengan struktur
yang sama dengan table DEPARTMENTS !

5. Gunakan perulangan (basic LOOP) untuk memasukkan data daritabel


DEPARTMENTS dengan department_id = 10, 20, 30, 40, 50, 60, 70, 80, 90,
100 ke dalam table yang sudah dibuat pada nomor 4 !

• Keterangan :
• NIM diisi dengan NIM sendiri

• subject : PLSQL KLS LAT06 NIM NAMA


TUGAS Latihan LOOP
1. Buat tabel dengan namaEMP_DEPT_NIM dengannama-namakolom
EMPLOYEE_ID, FIRST_NAME, DEPARTMENT_NAME !
2. Masukkan data pengawaidengan EMPLOYEE_ID = 100, 105, 110, 115,
120 kedalam table yang sudahdibuatpadanomer 3
denganmenggunakan WHILE
Summary
In this lesson, you should have learned how to:
Change the logical flow of statements by using the
following control structures.
• Conditional (IF statement)
• CASE expressions and CASE statements
• Loops:
– Basic loop
– FOR loop
– WHILE loop
• EXIT statements

You might also like