-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursor-week3.sql
More file actions
77 lines (69 loc) · 2.12 KB
/
Copy pathCursor-week3.sql
File metadata and controls
77 lines (69 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
--Question 1
DECLARE
v_dep_id employees.department_id%TYPE := &id;
CURSOR emp_cursor IS
SELECT *
FROM EMPLOYEES
WHERE department_id = v_dep_id
ORDER BY last_name;
emp_record emp_cursor%rowtype;
BEGIN
open emp_cursor;
LOOP
FETCH emp_cursor into emp_record;
dbms_output.put_line(emp_record.last_name || ' ' ||emp_record.first_name);
EXIT WHEN emp_cursor%NOTFOUND;
END LOOP;
CLOSE emp_cursor;
END;
--EXERCISE 3 NUMBER 2
DECLARE
CURSOR dep_cursor IS
SELECT department_id, department_name
FROM departments
ORDER BY department_id;
CURSOR emp_cursor IS
SELECT first_name, last_name, department_id
FROM employees
ORDER BY department_id;
BEGIN
FOR b in dep_cursor LOOP
dbms_output.put_line(b.department_id || ' ' || b.department_name);
FOR a in emp_cursor LOOP
IF a.department_id = b.department_id THEN
dbms_output.put_line('-- ' || a.first_name || ' ' || a.last_name);
END IF;
END LOOP;
END LOOP;
END;
--Number 3
DECLARE
CURSOR emp_cursor IS
SELECT salary
FROM employees
ORDER BY salary ASC; --important
shortSal NUMBER(8,2):=0;
salaryVar NUMBER(8,2):=0;
countVar INT:=0;
BEGIN
FOR a in emp_cursor LOOP
IF salaryVar < 15000 THEN
shortSal := (a.salary * 0.05);
a.salary := a.salary + shortSal;
--for make a total of salary raised
salaryVar := salaryVar + shortSal;
countVar := countVar + 1;
IF salaryVar > 15000 THEN
salaryVar := salaryVar - shortSal;
countVar := countVar - 1;
--decremented by the last input
END IF;
--for counting a total of employee raised
END IF;
END LOOP;
-- we need to let shortSal variable turned into 0 again
-- as a reusable variable
shortSal := 0;
DBMS_OUTPUT.PUT_LINE('Total money spent : ' || salaryVar);
DBMS_OUTPUT.PUT_LINE('Total Employee : ' || countVar);
END;