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

Declare Begin Select From Where End : Menampilkan Nama Pekerja Pada ID Pekerja Tertentu. Varchar2 Into

The document shows examples of PL/SQL code for retrieving, updating, and deleting data from tables. It also demonstrates various types of loops like basic, for, while, and array looping.

Uploaded by

Angga Hooligans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Declare Begin Select From Where End : Menampilkan Nama Pekerja Pada ID Pekerja Tertentu. Varchar2 Into

The document shows examples of PL/SQL code for retrieving, updating, and deleting data from tables. It also demonstrates various types of loops like basic, for, while, and array looping.

Uploaded by

Angga Hooligans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Menampilkan Nama Pekerja pada ID pekerja tertentu.

DECLARE
name VARCHAR2(50);
BEGIN
SELECT first_name || ||last_name INTO name
FROM employees
WHERE employee_id = 100;
END;
Menampilkan tanggal dan gaji pegawai pada id tertentu.
DECLARE

emp_hiredate employees.hire_date%TYPE;
emp_salary employees.salary%TYPE;
BEGIN
SELECT hire_date, salary
INTO emp_hiredate, emp_salary
FROM employees
WHERE employee_id = 100;
END;
/
Menampilkan total gaji pegawai pada department tertentu tertentu.
SET SERVEROUTPUT ON
DECLARE
sum_sal NUMBER(10,2);

deptno NUMBER NOT NULL := 60;


BEGIN
SELECT SUM(salary) group function
INTO sum_sal FROM employees
WHERE department_id = deptno;
DBMS_OUTPUT.PUT_LINE (The sum of salary is || sum_sal);
END;
/
UPDATING DATA
DECLARE

sal_increase employees.salary%TYPE := 1000;


BEGIN
UPDATE employees
SET salary = salary + sal_increase
WHERE job_id = ST_CLERK;
END;

DELETING DATA
DECLARE

deptno employees.department_id%TYPE := 20;


BEGIN
DELETE FROM employees
WHERE department_id = deptno;
END;
/

IF-ELSE STATEMENT

SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
ANGKA NUMBER :=&n;
BEGIN
IF Angka > 10 THEN
DBMS_OUTPUT.PUT_LINE('ANGKA' || ANGKA || 'LEBIH BESAR DARI
10');
ELSIF ANGKA < 10 THEN
DBMS_OUTPUT.PUT_LINE('ANGKA' || ANGKA || 'LEBIH KECIL DARI
10');
ELSE DBMS_OUTPUT.PUT_LINE('ANGKA' || ANGKA || 'SAMA DENGAN
10');
END IF;
END;

Basic Loop
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;
/

FOR LOOP

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;
/

While Loops
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;
/

Looping Angak 20 s/d 1 Secara Menurun


Set serveroutput on
Declare
J integer;
Begin
J := &J;
For J in reverse 1..20 loop
Dbms_output.put_line (J);
End loop;
End;

Looping Membuat segitiga


Set serveroutput on
Declare
I integer;
J integer;
Begin
For I in 1..10 loop
For J in 1..I loop
Dbms_output.put (J);
End loop;
Dbms_output.put_line ( );
End loop;
End;
Looping Membuat segitiga terbalik
Set serveroutput on
Declare
I integer;

J integer;
Begin
For I in reverse 1..10 loop
For J in 1..I loop
Dbms_output.put (J);
End loop;
Dbms_output.put_line ( );
End loop;
End;

Looping Elemen Larik Array


Set serveroutput on
Declare type larik is
Table of number
Index by binary_integer;
A larik;
Begin
For I in 1..5 loop
A(I) := I * 10;
End loop;
For I in 1..5 loop
Dbms_output.put_line (Nilai Elemen Larik Ke- || I || : || A(I));
End loop;
End;

You might also like