You are on page 1of 6

set serveroutput on

--1) first method

DECLARE
dept_id NUMBER(4);
dept_name VARCHAR2(30);
man_id NUMBER(6);
loc_id NUMBER(4);
BEGIN
INSERT INTO departments VALUES ( 123,'forest',100,1700 )
RETURNING department_id ,department_name,manager_id ,location_id INTO
dept_id,dept_name ,man_id,loc_id;

IF SQL%ROWCOUNT >0 THEN


COMMIT;
dbms_output.put_line('inserted department is '||dept_id||' having '||'
dept_name= '||dept_name||' ,manager_id= '||man_id||' and location_id= '||loc_id);
END IF;
END;

OUTPUT :

inserted department is 123 having dept_name='forest' ,manager_id= 100 and


location_id= 1700

PL/SQL procedure successfully completed.

--1) second method using procedure

create or replace PROCEDURE NewDepartment(dept_id IN NUMBER,dept_name IN


VARCHAR2,man_id IN NUMBER,loc_id IN NUMBER) IS

BEGIN
INSERT INTO departments VALUES(dept_id,dept_name,man_id,loc_id);
IF SQL%ROWCOUNT >0 THEN
COMMIT;
dbms_output.put_line('inserted department is '||dept_id||' having '||'
dept_name= '||dept_name||' ,manager_id= '||man_id||' and location_id= '||loc_id);
END IF;
END;

BEGIN
NewDepartment(123,'forest',100,1700);
END;

OUTPUT :

inserted department is 123 having dept_name= forest ,manager_id= 100 and


location_id= 1700

PL/SQL procedure successfully completed.

--2)

DECLARE
name varchar2(30);
BEGIN
select department_name into name from departments where department_id=123;
delete from departments where department_id=123;
DBMS_OUTPUT.PUT_LINE('the department name '||name||' deleted');
END;

OUTPUT

the department name Treasury deleted

PL/SQL procedure successfully completed.

--3)

DECLARE
name varchar2(30);
BEGIN
select department_name into name from departments where department_id=120;
update departments set manager_ID=145 where department_id=120;
DBMS_OUTPUT.PUT_LINE('the department name '||name||' updated');
END;

OUTPUT

the department name Treasury updated

PL/SQL procedure successfully completed.

--4)

DECLARE
cnt number := 0;
dept_size varchar2(20);
BEGIN
select count(*) into cnt from EMPLOYEES where department_id=80;
if cnt>=30 then
dept_size:='LARGE';
elsif cnt>=8 then
dept_size:='MEDIUM';
else
dept_size:='SAMLL';
end if;
DBMS_OUTPUT.PUT_LINE('the department name '||dept_size||' is '||dept_size);
END;

OUTPUT

the department name LARGE is LARGE

PL/SQL procedure successfully completed.

--5)
DECLARE
bonus number := 0;

cursor cur1 IS
SELECT employee_id, salary,
floor (months_between (sysdate, hire_date)/12) year
FROM employees where EMPLOYEE_ID=205;
tuple cur1%ROWTYPE;
BEGIN
open cur1;
loop
fetch cur1 into tuple;
bonus:=1500;
if tuple.year>=27 then
bonus:=bonus+900;
elsif tuple.year=25 or tuple.year=26 then
bonus:=bonus+800;
end if;

if tuple.salary>10000 then
bonus:=bonus+1200;
elsif tuple.salary>=5000 then
bonus:=bonus+1000;
else
bonus:=bonus+800;
end if;
exit when cur1%NOTFOUND;
end loop;
close cur1;
DBMS_OUTPUT.PUT_LINE('the bonus of employee number '||tuple.employee_id||' is '||
bonus);
END;

OUTPUT

the bonus of employee number 205 is 3600

PL/SQL procedure successfully completed.

--6)

DECLARE
v NUMBER :=35;
BEGIN
LOOP
dbms_output.put_line(v);
EXIT WHEN v=37;
v:=v+1;
END LOOP;
END;

OUTPUT
35
36
37
PL/SQL procedure successfully completed.

BEGIN
FOR i in 35..37 LOOP
dbms_output.put_line(i);
END LOOP;
END;

OUTPUT
35
36
37

PL/SQL procedure successfully completed.

DECLARE
v NUMBER :=35;
BEGIN
WHILE v<=37 LOOP
dbms_output.put_line(v);
v:=v+1;
END LOOP;
END;

OUTPUT
35
36
37

PL/SQL procedure successfully completed.

--7)
DECLARE
i integer := 1;
j integer := 1;
BEGIN
FOR i in 1..9 LOOP
DBMS_OUTPUT.PUT('|');
FOR j in 1..9 LOOP
DBMS_OUTPUT.PUT (' ' || i || j || ' ');
END loop;
j := 1;
DBMS_OUTPUT.PUT_LINE('|');
END loop;
END;

OUTPUT

| 11 12 13 14 15 16 17 18 19 |
| 21 22 23 24 25 26 27 28 29 |
| 31 32 33 34 35 36 37 38 39 |
| 41 42 43 44 45 46 47 48 49 |
| 51 52 53 54 55 56 57 58 59 |
| 61 62 63 64 65 66 67 68 69 |
| 71 72 73 74 75 76 77 78 79 |
| 81 82 83 84 85 86 87 88 89 |
| 91 92 93 94 95 96 97 98 99 |

PL/SQL procedure successfully completed.


--8)

BEGIN
UPDATE employees
SET salary=salary+0.2 * salary
WHERE department_id=20 and salary<=1500;

dbms_output.put_line(SQL%ROWCOUNT||' rows updated');

DELETE FROM employees WHERE department_id=30;

dbms_output.put_line(SQL%ROWCOUNT||' rows deleted');


ROLLBACK;
END;

OUTPUT :

5 rows updated
1 rows deleted

--9)

DECLARE
CURSOR cur2 is
SELECT employee_id,(first_name||' '||last_name) as full_name,department_id
FROM employees WHERE manager_id=114 ;
tuple cur2%ROWTYPE;
BEGIN
OPEN cur2;
LOOP
FETCH cur2 INTO tuple;
dbms_output.put_line('emp_id ='||tuple.employee_id||' full_name='||
tuple.full_name||' department id='||tuple.department_id);
exit when cur1%NOTFOUND;
END LOOP;
CLOSE cur2;
END;

OUTPUT

emp_id =115 full_name=Alexander Khoo department id=30


emp_id =116 full_name=Shelli Baida department id=30
emp_id =117 full_name=Sigal Tobias department id=30
emp_id =118 full_name=Guy Himuro department id=30
emp_id =119 full_name=Karen Colmenares department id=30
emp_id =119 full_name=Karen Colmenares department id=30
PL/SQL procedure successfully completed.

You might also like