mysql stored procedures, functions, and simple application

1. What is a stored procedure, function

  Stored procedures and functions are supported from Version 5.0, both in the definition is very similar, are in advance through a set of SQL statements in the database compilation and storage, the equivalent method in java.

2, the benefits of stored procedures, functions

  1, for developers, can greatly simplify their work.

  2, the relevant data are processed on the database side, thus reducing the transmission between the application and the database server, data processing efficiency is improved.

3, the difference between the stored procedures and functions

  1, the function has to return the stored procedure no.

  2, may be a parameter of a procedure IN, OUT or INOUT type and function type can be IN.

4, small cases stored procedure

  The following mini case is implemented in Navicat.

  Before writing case to prepare a simple form:

CREATE TABLE `users` (
  `u_id` int(4) NOT NULL AUTO_INCREMENT,
  `u_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  `u_age` int(4) DEFAULT NULL,
  PRIMARY KEY (`u_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

 

  

  Inserted more than a few data.

  In step Navicat stored in the new procedure: Right-click the 'function' -> New Functions -> Process -> select parameters -> complete

  1, to create a no-argument

CREATE DEFINER=`root`@`localhost` PROCEDURE `noParamName_select`()
BEGIN
    #Routine body goes here...
    select u_name as '用户名' from users where u_id = 1;
END

 

  We just need to write code between BEGIN and END like other Navicat will create good for us.

# No arguments
call noParamName_select (); # call way is to use the keyword call

 

 

  2, IN type parameter

CREATE DEFINER=`root`@`localhost` PROCEDURE `inParamName_select`(IN `user_id` int)
BEGIN
    #Routine body goes here...
    select u_name as '用户名' from users where u_id = user_id;
END

 

  The effect of changing process is entered user id, user name corresponding to the query

#IN parameters
call inParamName_select(2);

 

  3, OUT type parameter

CREATE DEFINER=`root`@`localhost` PROCEDURE `outParamName_select`(OUT `user_name` varchar(20))
BEGIN
    #Routine body goes here...
    select u_name into user_name from users where u_id = 2;
END

 

  Here we must note that the parameter type must be added to the back of varchar length, otherwise an error occurs

#OUT id parameter check
call outParamId_select(@a);
select @a;

 

  For OUT type parameters, but also pass parameters when calling the function, use @ at the beginning, because OUT type parameter is used as an output, the final use select queries.

  4, together with the IN and OUT type parameters Parameter Type

CREATE DEFINER=`root`@`localhost` PROCEDURE `inAndOutParamName_select`(IN `user_id` int,OUT `user_name` varchar(20))
BEGIN
    #Routine body goes here...
    select u_name into user_name from users where u_id = user_id;
END

 

# Use both IN and OUT parameters to query the user name
inAndOutParamName_select Call ( . 1 , @n );
 SELECT  @n  AS  ' username ' ;

 

  5, INOUT parameters, both as inputs and as outputs.

BEGIN
    #Routine body goes here...
    SET  @number  = NUM; # set the variable
     SELECT  @number  +  10  INTO NUM;
 the END
#INOUT parameter
 SET  @num  =  2 ;
call inoutParamName_select(@num);
select @num;

 

Guess you like

Origin www.cnblogs.com/aixinyiji/p/11028970.html