您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何编写一个 MySQL 存储函数来更新表中的值?

2024/3/28 9:10:19发布18次查看
众所周知,当我们想要返回结果时,最好使用函数。因此,当我们创建存储函数来操作表(例如插入或更新值)时,它或多或少类似于存储过程。在下面的示例中,我们将创建一个名为“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 存储函数来更新表中的值?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product