MySQL IF语句如何在存储过程中使用?
MySQL IF语句在存储过程中实现了基本的条件构造。它的语法如下-
IF expression THENStatements;
END IF;
它必须以分号结尾。为了演示IF语句在MySQL存储过程中的使用,我们创建了以下存储过程,该存储过程基于如下所示的名为“ student_info”的表的值-
mysql> Select * from student_info;+------+---------+------------+------------+
| id | Name | Address | Subject |
+------+---------+------------+------------+
| 101 | YashPal | Amritsar | History |
| 105 | Gaurav | Jaipur | Literature |
| 125 | Raman | Shimla | Computers |
+------+---------+------------+------------+
3 rows in set (0.00 sec)
以下查询将创建一个名为“ coursedetails_IF”的过程,该过程中包含IF语句-
mysql> DELIMITER // ;mysql> CREATE PROCEDURE coursedetails_IF(IN S_Subject Varchar(20), OUT S_Course varchar(20))
-> BEGIN
-> DECLARE Sub Varchar(20);
-> SELECT Subject INTO SUB
-> FROM Student_info WHERE S_Subject = Subject;
-> IF Sub = 'Computers' THEN
-> SET S_Course = 'B.Tech(CSE)';
-> END IF;
-> END //
现在,当我们调用此过程时,我们可以看到以下结果:
mysql> Delimiter ; //mysql> CALL coursedetails_IF('Computers', @S_Course);
mysql> Select @S_Course;
+-------------+
| @S_Course |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)
以上是 MySQL IF语句如何在存储过程中使用? 的全部内容, 来源链接: utcz.com/z/321940.html