
众所周知,当我们想要返回结果时,最好使用函数。因此,当我们创建存储函数来操作表(例如插入或更新值)时,它或多或少类似于存储过程。在下面的示例中,我们将创建一个名为“tbl_update”的存储函数,它将更新名为“student_marks”的表中的值。
mysql> Select * from student_marks//
+---------+------+---------+---------+---------+
| Name | Math | English | Science | History |
+---------+------+---------+---------+---------+
| Raman | 95 | 89 | 85 | 81 |
| Rahul | 90 | 87 | 86 | 81 |
| Mohit | 90 | 85 | 86 | 81 |
| Saurabh | NULL | NULL | NULL | NULL |
+---------+------+---------+---------+---------+
4 rows in set (0.00 sec)
mysql> Create Function tbl_Update(S_name Varchar(50),M1 INT,M2 INT,M3 INT,M4 INT)
-> RETURNS INT
-> DETERMINISTIC
-> BEGIN
-> UPDATE student_marks SET Math = M1,English = M2, Science = M3, History =M4 WHERE Name = S_name;
-> RETURN 1;
-> END //
Query OK, 0 rows affected (0.03 sec)
mysql> Select tbl_update('Saurabh',85,69,75,82);
+------------------------------------+
| tbl_update('Saurabh',85,69,75,82) |
+------------------------------------+
| 1 |
+------------------------------------+
1 row in set (0.07 sec)
mysql> Select * from Student_marks;
+---------+------+---------+---------+---------+
| Name | Math | English | Science | History |
+---------+------+---------+---------+---------+
| Raman | 95 | 89 | 85 | 81 |
| Rahul | 90 | 87 | 86 | 81 |
| Mohit | 90 | 85 | 86 | 81 |
| Saurabh | 85 | 69 | 75 | 82 |
+---------+------+---------+---------+---------+
4 rows in set (0.00 sec)以上就是如何编写一个 MySQL 存储函数来更新表中的值?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号