|
||||||
MySQL Stored Procedures and FunctionsHow to Create Stored Procedures and Functions in a MySQL Database
Oracle, unlike MySQL, has stored procedures and functions available for the database developer, giving Oracle a major advantage. That's all changed with MySQL 5.
Over the past few years one major advantage that some databases, such as Oracle and Microsoft SQL Server, have had over MySQL is their ability to use stored functions and stored procedures. Well, that was before MySQL 5; with MySQL 5 a database developer can start adding in their own bespoke functionality. What are Stored Procedures and Stored Functions?Any programmers reading this should already be comfortable with the concept of subroutines and functions: encapsulated pieces of code that can be called by programs - often used to carry out repetative or complicated tasks. Subroutines and functions can be made available to a single program or many; and that, of course, is what stored procedures and stored functions are - they are procedures (or subroutines) and functions stored in the database. What's the Difference Between a Stored Procedure and a Stored Function?The difference between a stored procedures and stored functions is the same as the difference between a subroutine and a function:
Why Use Stored Procedures and Stored Functions?The real advantage to using stored procedures and stored functions is that they provide functionality which is platform and application independant. For example, a team of developers may provide:
Without stored procedures and stored functions then the functionality would have to be developed independently for each application, but with stored procedures and stored functions the functionality only has to be developed once. Creating MySQL Stored ProceduresA stored procedure is the same as a subroutine in that it cannot directly return a result, however it can receive variables that can be modified by the procedure; these variables are defined as:
Procedures are always declared in the same way:
One other (rather imporant) thing to bear in mind is that semicolons are used as part of the definition of the procedure. For this reason the end of line delimiter must be redefined to something that won't be used in the definition. For example: delimiter //
create procedure circle_area (in r double, out a double)
begin
set a = r * r * pi();
end
//
delimiter ;
Running MySQL Stored ProceduresA MySQL stored procedure is run by using the call method: call circle_area(22, @a);
select @a;
In this example 1520.5308443375 would be displayed on the screen. Creating MySQL Stored FunctionsUnlike stored procedures stored functions always return a result, they will also be one of two types:
Like procedures, all functions are created the same way:
For example: delimiter //
create function circumference (r double) returns double
deterministic
begin
declare c double;
set c = 2 * r * pi();
return c;
end
//
delimiter ;
Running MySQL Stored FunctionsUnlike stored procedures stored functions are used as part of a select statement: select circumference(22);
In this case (for anyone that's interested) the result would be 138.23007675795. ConclusionStored procedures and stored functions are important tools for any database developer, and thankfully, those tools are now available to MySQL users - provided, of course, that they're using MySQL 5.
The copyright of the article MySQL Stored Procedures and Functions in Database Programming is owned by Mark Alexander Bain. Permission to republish MySQL Stored Procedures and Functions in print or online must be granted by the author in writing.
Comments
Sep 25, 2008 12:36 PM
Guest :
Sep 26, 2008 2:51 AM
Mark Alexander Bain :
Oct 3, 2008 10:20 PM
Guest :
Jul 21, 2009 10:35 AM
Guest :
Aug 6, 2009 2:22 AM
Guest :
5 Comments
|
||||||
|
|
||||||
|
|
||||||