Stored procedure with IN Parameters

Here is the command to create a MySQL stored procedure with one IN parameter, here we are getting total number of employee by department, dept_id is a foreign key from department table.

mysql> DELIMITER //
mysql> create procedure usp_totalEmployeeByDeparment(IN id INT)
    -> begin
    -> select count(*) as total from employee where dept_id = id;
    -> end//

mysql> DELIMITER ;

We have first changed delimiter as // to mark end of stored procedure, and then reverted it back to previous delimiter. Also using “usp” as prefix for user defined stored procedure is one of the SQL best practices to separate system and user stored procedures. Now you can call this stored procedure from MySQL command prompt as :

mysql> call usp_totalEmployeeByDeparment(2);

Stored procedure with IN and OUT parameters

In this MySQL example, we have created stored procedure usp_GetEmployeeName which takes one IN and one OUT parameter. While calling this stored procedure, you need to pass two parameters, id and name. One would be  input parameter id and other would be output parameter to store result.

mysql> DELIMITER //
mysql> create procedure usp_GetEmployeeName(IN id INT, OUT name VARCHAR(20))
    -> begin
    -> select emp_name into name from employee where emp_id = id;
    -> end//

mysql> DELIMITER ;

mysql> call usp_GetEmployeeName(103, @name);


Kindly Share This Post »»

Responses

0 Respones to "Create and Call Stored Procedure in MySQL using IN and OUT parameters "

Post a Comment

 
HOME | Freshers | Exp | Java | SQL | Walkins | OffCampus | BankJobs
=*= PRIVACY POLICY And DISCLAIMER =*=
Some of the stuff contained this site are found on internetThis site is not responsible for publishing all available Information as well accuracy, please check posted Information with its original sources, e.g. News Papers/ Websites etc.We collect all the Information form the Internet.
Software My Zimbio
Copyright © 2013 Career+ Blog +Google | Disclaimer | Privacy Policy | Contact