Wednesday, February 17, 2010

Store Procedure and Function

The main different between store procedure and function is that
- Function can return only one value and must return a value
- Store procedure can have many IN or OUT parameters and no return value
- Function can be called in a select statement but store procedure can not be called in a select, it must be executed by 'call storeprocedure name'
Example
create function AddA(p_inparam varchar(30)) returns varchar(30)
return concat('A',p_inparam);

mysql> select AddA(emp_name) from emps;
+----------------+
| AddA(emp_name) |
+----------------+
| ARoger |
| AJohn |
| AAlan |
+----------------+
3 rows in set (0.26 sec)

create procedure AddB(INOUT p_inparam varchar(30))
set p_inparam := concat('B',p_inparam);

mysql> select AddB(emp_name) from emps;
ERROR 1305 (42000): FUNCTION pers.AddB does not exist

mysql> select call AddB(emp_name) from emps;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'call AddB(emp_name) from emps' at line 1

No comments:

Post a Comment