如何编写在表中插入值的 MySQL 存储函数?
众所周知,当想要返回结果时,函数最好用。因此,当我们创建存储函数来操作表(例如插入或更新值)时,将会或多或少地类似于存储过程。
示例
在以下示例中,我们创建一个名为“tbl_insert”的存储函数,它将一个名为“student_marks”的表中的值插入到表中。
mysql> Create Function tbl_insert(S_name Varchar(50),M1 INT,M2 INT,M3 INT,M4 INT) -> RETURNS INT -> DETERMINISTIC -> BEGIN -> INSERT INTO student_marks values(S_name,M1,M2,M3,M4); -> RETURN 1; -> END// Query OK, 0 rows affected (0.00 sec) mysql> Select tbl_insert('Saurabh',85,79,65,71); +------------------------------+ | tbl_insert('RR',58,25,65,32) | +------------------------------+ | 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 | NULL | 86 | 81 | | Saurabh | 85 | 79 | 65 | 71 | +---------+------+---------+---------+---------+ 4 rows in set (0.00 sec)
广告