MySQL--存储函数代码笔记

mysql> select * from customers$$
+-----+-----------+----------+-----------+--------------+
| id  | cust_name | cust_sex | cust_city | cust_address |
+-----+-----------+----------+-----------+--------------+
| 901 | 马腾      | F        | 广东      | 深圳         |
| 902 | 张三      | F        | 北京      | 朝阳         |
| 903 | 李四      | M        | 武汉      | 李家庄       |
| 904 | 张无忌    | M        | 光明顶    | 明教         |
| 905 | 索隆      | M        | 动漫      | 海贼王       |
| 906 | 悟空      | F        | 西游      | 花果山       |
| 909 | 万华      | F        | 长沙市    | 芙蓉区       |
+-----+-----------+----------+-----------+--------------+
7 rows in set (0.89 sec)

mysql> create function fu_test(cid int)
    -> returns char(5)
    -> deterministic
    -> begin
    -> declare sex char(5);
    -> select cust_sex into sex from customers
    -> where id = cid;
    -> if sex is null then
    -> return(select '没有该客户');
    -> else if sex = 'F' then
    -> return(select '女');
    -> else return(select '男');
    -> end if;
    -> end if;
    -> end $$
Query OK, 0 rows affected (0.00 sec)

mysql> select fu_test(904);
    -> $$
+--------------+
| fu_test(904) |
+--------------+
| 男           |
+--------------+
1 row in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/qq_37592750/article/details/82891999