w3resource

Execute a Stored Procedure to Retrieve Total Salary


Call the Stored Procedure to Calculate Total Salary

Write a MySQL query to call the CalculateTotalSalaryByDepartment stored procedure and retrieve the total salary.

Solution:

-- Declare a variable to store the total salary, initializing it to 0
SET @total_salary = 0;

-- Call the `CalculateTotalSalaryByDepartment` stored procedure
-- Pass the department ID (2) as input and store the result in @total_salary
CALL CalculateTotalSalaryByDepartment(2, @total_salary);

-- Retrieve and display the total salary for the specified department
SELECT @total_salary AS TotalSalary; 

Explanation:

  • Purpose of the Query:
    • The goal is to execute the stored procedure and retrieve the total salary.
  • Key Components:
    • CALL: Executes the stored procedure.
    • SELECT @total_salary: Retrieves the output parameter.
  • Why use Stored Procedures?:
    • Stored procedures simplify complex operations and improve code reusability.
  • Real-World Application:
    • For example, in a payroll system, you might call a stored procedure to calculate total salaries by department.

For more Practice: Solve these Related Problems:

  • Write a MySQL query to call a stored procedure that calculates the total budget for a specific department.
  • Write a MySQL query to call a stored procedure that calculates the total revenue for a specific product category.
  • Write a MySQL query to call a stored procedure that calculates the total sales for a specific customer.
  • Write a MySQL query to call a stored procedure that calculates the total cost for a specific project.

Go to:


PREV :Stored Procedure to Calculate Total Salary by Department.
NEXT : Create a Trigger to Enforce Maximum Salary.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.