Computer >> Máy Tính >  >> Lập trình >> MySQL

Triển khai If else trong thủ tục được lưu trữ trong MySQL?

Để triển khai if-else, cú pháp như sau -

if yourCondition then
     yourStatement1;
    else
    yourStatement2;
    end if ;

Để hiểu khái niệm ở trên cho if-else trong một thủ tục được lưu trữ, hãy để chúng tôi tạo một thủ tục được lưu trữ -

mysql> delimiter //
mysql> create procedure If_else_stored_demo(value int)
     begin
     if value > 1000 then
     select "your value is greater than 1000";
     else
     select "your value is less than or equal to 1000";
     end if ;
     end
     //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

Bây giờ bạn có thể gọi thủ tục đã lưu trữ bằng lệnh gọi -

mysql> call If_else_stored_demo(500);

Điều này sẽ tạo ra kết quả sau -

+------------------------------------------+
| your value is less than or equal to 1000 |
+------------------------------------------+
| your value is less than or equal to 1000 |
+------------------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)