MySQL stored procedure usage--including exercises

MySQL version 5.0 began to support stored procedures.

A stored procedure (Stored Procedure) is a database object that stores complex programs in the database for external program calls.

A stored procedure is a set of SQL statements to complete a specific function. It is compiled and saved in the database. Users can call and execute the stored procedure by specifying the name of the stored procedure and giving parameters (when necessary).

The stored procedure is very simple in thought, it is the code encapsulation and reuse at the SQL language level of the database.

The following examples are all demonstrated with the emp table. The data of the emp table can be obtained here  https://blog.csdn.net/wuyomhchang/article/details/124000402?spm=1001.2014.3001.5501

-- 变量的基本用法
SELECT  MAX(sal) into @maxsal FROM emp;
SELECT @maxsal;
SELECT * FROM emp WHERE sal=@maxsal;

1. Syntax of stored procedure

-- 存储过程
DROP PROCEDURE IF EXISTS get_maxsal; --(自定义存储过程的名字)
DELIMITER//
CREATE PROCEDURE get_maxsal()
 BEGIN
   SELECT max(sal) FROM emp;    --(begin-end写要执行的代码)
  END//
	
CALL get_maxsal();  --调用存储过程
   

2. Declare and use variables in stored procedures

-- 在存储过程中声明并使用变量
DROP PROCEDURE IF EXISTS get_sal();
DELIMITER//
CREATE PROCEDURE get_sal()
 BEGIN
   DECLARE avgsal INT;
	 SELECT AVG(sal) into avgsal FROM emp;
	 SELECT ename,sal FROM emp WHERE sal > avgsal;
 END
 
CALL get_sal();

3. Stored procedure syntax with parameters

IN/OUT/INOUT param_name param_type(param_size)

-- IN OUT INOUT为参数类型

IN input parameter: Indicates that the value of the parameter must be assigned when  calling the stored procedure
, and the value of the parameter modified in the stored procedure cannot be returned, and is the default value OUT output parameter: The value can be changed inside the stored procedure and can be Return 
INOUT input and output parameters: specific values ​​must be specified when calling, and can be changed in the stored procedure and can be returned after the call

 Look at a little chestnut!

DROP PROCEDURE IF EXISTS get_sal();
DELIMITER//
CREATE PROCEDURE get_sal(IN empname VARCHAR(5),OUT empsal INT)
 BEGIN
   SELECT sal INTO empsal from emp WHERE ename=empname;
 END//
 
 -- 在存储过程中定义的参数,会在mysql中自动生成以下划线命名的参数
 CALL get_sal('BLACK',@_empsal);
 SELECT @_empsal;
 

4. Process control function (similar to those in Java and python)

function name effect
IF Judgment, Process Control
IFNULL Determine whether it is empty
CASE search phrase

1. if judgment 

 -- if判断
DROP PROCEDURE IF EXISTS get_sal;
DELIMITER//
CREATE PROCEDURE get_sal(IN empname VARCHAR(5),OUT sallevel VARCHAR(10))
BEGIN
  DECLARE empsal INT;
	SELECT sal INTO empsal FROM emp WHERE ename=empname;
	IF empsal>3000 THEN
	 SET sallevel='high';
	ELSEIF empsal<=3000 AND empsal>2000 THEN
	 SET sallevel='middle';
  ELSE
	 SET sallevel='low';
  END if;
END//

CALL get_sal('BLACK',@sallevel);
SELECT @sallevel;

2. case syntax

effect

  • Advanced version of if, similar to switch ... case in Java

  • Match the value corresponding to the case through the conditional expression, and then perform the corresponding operation

grammar

-- only one conditional expression

CASE
    WHEN <condition 1> THEN <operation>
    WHEN <condition 2> THEN <operation>
    ...
    ELSE <operation>
END;

  • Compare the value of <expression> one by one with the <value> of each when

  • If you want to wait with a certain <value>, execute the <operation> behind it, if all when values ​​do not match, execute the else operation

  • If the value of when does not match and else is not written, an error will be reported

 Look at a chestnut!

-- case语法
CREATE PROCEDURE p()
  BEGIN
	 DECLARE v INT DEFAULT 1;
	 case v 
	  WHEN 2 THEN SELECT v;
		WHEN 3 THEN SELECT 0;
	 ELSE
	  BEGIN
		 SELECT 'hello';
	  END;
	END case;
	
END

CALL p();

 3. Loop REPEAT syntax

-- 基本语法 固定搭配
REPEAT
	statement_list
UNTIL search_condition END REPEAT; -- UNTIL后为结束循环的条件

Look at a chestnut! (prints the sum of the specified start and end numbers)

-- 打印指定开始和结束 数字的和 
DROP PROCEDURE if EXISTS dorepeat;
CREATE PROCEDURE dorepeat(in b INT,in e INT)
 BEGIN
   DECLARE total INT DEFAULT 0;
	 DECLARE temp INT DEFAULT b;
	 REPEAT
	   set total=total+temp;
		 set temp=temp+1;
UNTIL temp>e END REPEAT;
        SELECT total;
END

CALL dorepeat(1,10);
-- 例子2  应输出5 4 3 2 1
CREATE PROCEDURE dowhile()
BEGIN
   DECLARE num int DEFAULT 5;
	 WHILE num>0 DO
	   SELECT num;
		 SET num=num-1;
		END WHILE;
END 

CALL dowhile();

Another question! ! Write a stored procedure that accepts a department number as a parameter and finds the total number of employees in that department.

DELIMITER//
CREATE PROCEDURE get_total (IN bianhao INT,OUT _total INT) 
 BEGIN
   SELECT count(*) INTO _total FROM emp WHERE deptno=bianhao; 
  END
	
CALL get_total(10,@emptotal);
SELECT @emptotal;

This is the end of today's sharing, because the school has a lot of things recently, so the update will be a bit slow! Thank you for your support ♥

Guess you like

Origin blog.csdn.net/wuyomhchang/article/details/124332544