In PostgreSQL, create a PL/pgSQL block that defines a procedure namedupdate_employee_salary which takes two parameters: employee_id and new_salary. This procedure should update the salary ofthe employee with the given employee_id to the new salary value. Assume “employee” table with fields – eid, ename and salary
Question
In PostgreSQL, create a PL/pgSQL block that defines a procedure namedupdate_employee_salary which takes two parameters: employee_id and new_salary. This procedure should update the salary ofthe employee with the given employee_id to the new salary value. Assume “employee” table with fields – eid, ename and salary
Solution
Sure, here is a step-by-step guide on how to create a PL/pgSQL block that defines a procedure named update_employee_salary:
-
Open your PostgreSQL database interface.
-
Start by defining the procedure. In PostgreSQL, you use the
CREATE OR REPLACE PROCEDUREstatement to create a new procedure or replace an existing one. -
Specify the name of the procedure, which in this case is
update_employee_salary. -
Define the parameters that the procedure will take. In this case, it's
employee_idandnew_salary. -
Use the
LANGUAGEclause to specify the language name that the procedure is implemented in. In this case, it'splpgsql. -
Use the
ASclause to provide the code block that implements the procedure. In this case, it's anUPDATEstatement that updates thesalarycolumn of theemployeetable for the row that has theeidequal to theemployee_idparameter.
Here is the SQL code that implements the above steps:
CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id INT, new_salary NUMERIC)
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE employee
SET salary = new_salary
WHERE eid = employee_id;
END;
$$;
To call this procedure, you would use the CALL statement like this:
CALL update_employee_salary(1, 50000);
This would update the salary of the employee with eid of 1 to 50000.
Similar Questions
Create PL/pgSQL procedure for the increment of employees where in salary less than 35000 will get hike of 15% in their previoussalary and other will get 10% hike in their previous salary. Using following schema, Employees (id, name, department, salary)call the procedure by id and print employee’s name with their updated salary
In PostgreSQL, create a PL/pgSQL block that defines a function named calculate_salary_bonus which takes two parameters:employee_id and bonus_percentage. This function should calculate the bonus amount for the employee with the givenemployee_id based on their current salary and the provided bonus percentage. Assume “employee” table with fields – eid, enameand salary
For the Employee Class Program in this tutorial, what if we need the capability to update the salary for employees? What information is needed to perform this task, and what code segment implements obtaining it in a method?a.)Employees file information and the new salarydef upd_salary(): employees = read_employees() new_sal = input("Enter the new salary of the employee: ")b.)The new salarydef upd_salary(employees): new_sal = input("Enter the new salary of the employee: ")c.)Employee iddef upd_salary(employees): empid = input("Enter the employee ID: ")d.)Employee id and the new salarydef upd_salary(employees): empid = input("Enter the employee ID: ") new_sal = input("Enter the new salary of the employee: ")
A) Write PL/pgSQL function to increment the employee’s salary by 10% if his/her department is ‘HR’ for given(inputted)employee_id.Schema: Employee(employee_id, first_name, last_name, department, salary)B) Write PL/pgSQL function to find the number of Sundays between given dates
Given a table salary, such as the one below, that has m=male and f=femalevalues. Swap all f and m values (i.e., change all f values to m and vice versa)with a single update statement and no intermediate temp table.Note : You must write a single update statement, DO NOT write any selectstatement for this problem.Example:idnamesexsalary1Am25002Bf15003Cm55004Df500After running your update statement, the above salary table should have thefollowing rows:idnamesexsalary1Af25002Bm15003Cf55004Dm500Optionsupdate salaryset sex = if(sex = 'm', 'f', 'm');update salaryset sex = if(sex = 'm', 'm', 'm');update nameset salary = if(name = 'm', 'f', 'm');update salaryset sex = if(sex != 'f', 'm', 'm');
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.