Hướng dẫn create function mysql
13.1.17 CREATE PROCEDURE and CREATE FUNCTION Statements
These statements are used to create a stored routine (a stored procedure or function). That is, the specified routine becomes known to the server. By default, a stored routine is associated with the default database. To associate the routine explicitly with a given database, specify the name as The To invoke a stored procedure, use the By default, MySQL automatically grants the The If the routine name is the same as the name of a built-in SQL function, a syntax error occurs unless you use a space between the name and the following parenthesis when defining the routine or invoking it later. For this reason, avoid using the names of existing SQL functions for your own stored routines. The If a built-in function with the same name already exists, attempting to create a stored function with If a loadable function with the same name already exists, attempting to create a stored function using See Function Name Resolution, for more information. The parameter list enclosed within parentheses must always be present. If there are no parameters, an empty parameter list of Each parameter is an Note Specifying a parameter as An For each For information about the effect of unhandled conditions on procedure parameters, see Section 13.6.7.8, “Condition Handling and OUT or INOUT Parameters”. Routine parameters cannot be referenced in statements prepared within the routine; see Section 25.8, “Restrictions on Stored Programs”. The following example shows a simple stored procedure that, given a country code, counts the number of cities for that country that appear in the
The example uses the
mysql client The The following example function takes a parameter, performs an operation using an SQL function, and returns the result. In this case, it is unnecessary to use
Parameter types and function return types can be declared to use any valid data type. The
The MySQL permits routines to contain DDL statements, such as Statements that return a result set can be used within a stored procedure but not within a stored function. This prohibition includes
For additional information about statements that are not permitted in stored routines, see Section 25.8, “Restrictions on Stored Programs”. For information about invoking stored procedures from within programs written in a language that has a MySQL interface, see Section 13.2.1, “CALL Statement”. MySQL stores the The switch from the SQL mode of the invoker to that of the routine occurs after evaluation of arguments and assignment of the resulting values to routine parameters. If you define a routine in strict SQL mode but invoke it in nonstrict mode, assignment of arguments to routine parameters does not take place in strict mode. If you require that expressions passed to a routine be assigned in strict SQL mode, you should invoke the routine with strict mode in effect. The The A routine is considered “deterministic” if it always produces the same result for the same input parameters, and “not deterministic” otherwise. If neither Assessment of the nature of a routine is based on the “honesty” of the creator: MySQL does not check that a routine declared If binary logging is enabled, the A routine that contains the
Several characteristics provide information about the nature of data use by the routine. In MySQL, these characteristics are advisory only. The server does not use them to constrain what kinds of statements a routine is permitted to execute.
The The If the If the Within the body
of a stored routine that is defined with the Consider the following procedure, which displays a count of the number of MySQL accounts listed in the
The procedure is assigned a Now suppose that the procedure is defined with the
The procedure still has a By default, when a routine with the The server handles the data type of a routine parameter, local routine variable created with
|