Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the differences between stored procedures, functions and routines?

In MySQL database context, what is the difference among these 3 terms:

  • stored procedure
  • stored function
  • stored routine

Also the build-in functions like those date time functions (e.g. WEEKDAY() etc) are considered as what?

like image 452
Yang Avatar asked Apr 21 '10 06:04

Yang


People also ask

What is the difference between functions and stored procedures?

The function must return a value but in Stored Procedure it is optional. Even a procedure can return zero or n values. Functions can have only input parameters for it whereas Procedures can have input or output parameters. Functions can be called from Procedure whereas Procedures cannot be called from a Function.

What is the difference between function and routine?

A procedure is a routine that can accept arguments but does not return any values. A function is a routine that can accept arguments and returns one or more values. User-defined routine (UDR) is a generic term that includes both user-defined procedures and user-defined functions.

What is the difference between functions and procedures in SQL?

A function returns a value and control to calling function or code. A procedure returns the control but not any value to calling function or code. A procedure has support for try-catch blocks. A select statement can have a function call.

What is difference between stored procedure and function and view?

A view represents a virtual table. You can join multiple tables in a view and use the view to present the data as if the data were coming from a single table. A stored procedure uses parameters to do a function... whether it is updating and inserting data, or returning single values or data sets.


1 Answers

Google is your friend. The first match for "mysql routine function procedure" is this: http://dev.mysql.com/doc/refman/5.0/en/stored-routines-syntax.html

A quick summary:

A stored routine is either a procedure or a function.

A procedure is invoked using a CALL statement and can only pass back values using output variables.

A function can be called from inside a statement just like any other function and can return a scalar value.

like image 82
Jakob Avatar answered Sep 27 '22 17:09

Jakob