Professional Documents
Culture Documents
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;
OUTPUT :
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 :
--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
--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
--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
--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
--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
DECLARE
v NUMBER :=35;
BEGIN
WHILE v<=37 LOOP
dbms_output.put_line(v);
v:=v+1;
END LOOP;
END;
OUTPUT
35
36
37
--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 |
BEGIN
UPDATE employees
SET salary=salary+0.2 * salary
WHERE department_id=20 and salary<=1500;
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