Home > Database > Mysql Tutorial > body text

How is the MySQL CASE statement used in stored procedures?

WBOY
Release: 2023-08-22 13:37:07
forward
636 people have browsed it

MySQL CASE语句如何在存储过程中使用?

In fact, the CASE statement has the function of the IF-THEN-ELSE statement. Its syntax is as follows:

CASE
WHEN condition_1 THEN
   {...statements to execute when condition_1 is TRUE...}
[ WHEN condition_2 THEN
   {...statements to execute when condition_2 is TRUE...} ]
[ WHEN condition_n THEN
   {...statements to execute when condition_n is TRUE...} ]
[ ELSE
   {...statements to execute when all conditions were FALSE...} ]
END CASE;
Copy after login

The CASE statement will execute the ELSE clause when no WHEN clause is executed.

To demonstrate the use of CASE statements in MySQL stored procedures, we have created the following stored procedure based on table 'student_info' with values ​​as follows:

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)
Copy after login

The following query will create A stored procedure named 'coursedetails_CASE' which contains a CASE statement −

mysql> Delimiter //
mysql> CREATE PROCEDURE coursedetails_CASE(IN S_subject Varchar(20), OUT S_Course Varchar(50))
   -> BEGIN
   -> DECLARE SUB VArchar(20);
   -> SELECT SUBJECT INTO Sub
   -> FROM Student_Info WHERE S_Subject = Subject;
   -> CASE S_Subject WHEN 'Computers' THEN
   -> SET S_Course = 'B.Tech(CSE)’;
   -> WHEN 'History' THEN
   -> SET S_Course = 'Masters in History';
   -> WHEN 'Literature' THEN
   -> SET S_Course = 'Masters in English';
   -> ELSE
   -> SET S_Course = 'Subject not in the table';
   -> END CASE ;
   -> END //
Query OK, 0 rows affected (0.11 sec)
Copy after login

Now when we call this procedure we can see the following result −

mysql> DELIMITER ;
mysql> CALL coursedetails_CASE ('Computers', @S_course);
Query OK, 1 row affected (0.08 sec)

mysql> Select @S_Course;
+-------------+
| @S_Course   |
+-------------+
| B.Tech(CSE) |
+-------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_CASE ('literature', @S_course);
Query OK, 1 row affected (0.00 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in English |
+--------------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_CASE ('Math', @S_course);
Query OK, 0 rows affected (0.00 sec)

mysql> Select @S_Course;
+--------------------------------+
| @S_Course                      |
+--------------------------------+
| Subject Not in the table       |
+--------------------------------+
1 row in set (0.00 sec)

mysql> CALL coursedetails_CASE ('History', @S_course);
Query OK, 1 row affected (0.01 sec)

mysql> Select @S_Course;
+--------------------+
| @S_Course          |
+--------------------+
| Masters in History |
+--------------------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of How is the MySQL CASE statement used in stored procedures?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!