Skip to content

Commit e51d347

Browse files
[ADD] Function sample using different steps
1 parent a01e267 commit e51d347

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

classes/32-functions.sql

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,36 @@ SELECT
6060
first_name,
6161
max_raise(employee_id)
6262
FROM employees;
63+
64+
-- max_raise_2 function
65+
CREATE OR REPLACE FUNCTION max_raise_2(empl_id INT)
66+
RETURNS NUMERIC(8,2) AS $$
67+
DECLARE
68+
employee_job_id INT;
69+
current_salary NUMERIC(8,2);
70+
job_max_salary NUMERIC(8,2);
71+
possible_raise NUMERIC(8,2);
72+
BEGIN
73+
-- Take the job position and the salary
74+
SELECT job_id, salary INTO employee_job_id, current_salary
75+
FROM employees
76+
WHERE employee_id = empl_id;
77+
78+
-- Take the max salary based on his job position
79+
SELECT max_salary INTO job_max_salary
80+
FROM jobs
81+
WHERE job_id = employee_job_id;
82+
83+
-- Calculations
84+
possible_raise = job_max_salary - current_salary;
85+
86+
RETURN possible_raise;
87+
END;
88+
$$ LANGUAGE plpgsql;
89+
90+
SELECT *
91+
FROM max_raise_2(206);
92+
93+
SELECT *
94+
FROM employees
95+
WHERE employee_id = 206;

0 commit comments

Comments
 (0)