在MySQL中通过一个存储过程调用在两个表中插入值
以下是使用存储过程在两个表中插入值的语法-
DELIMITER // CREATE PROCEDURE yourProcedureName(anyVariableName int) BEGIN insert into yourTableName1(yourColumnName1) values(yourVariableName); insert into yourTableName2(yourColumnName2) values(yourVariableName); END //
让我们首先创建一个表-
mysql> create table DemoTable1 -> ( -> StudentScore int -> );
以下是第二张表-
mysql> create table DemoTable2 -> ( -> PlayerScore int -> );
这是创建存储过程并在两个表中插入值的查询-
mysql> DELIMITER // mysql> CREATE PROCEDURE insert_proc(value int ) -> BEGIN -> insert into DemoTable1(StudentScore) values(value); -> insert into DemoTable2(PlayerScore) values(value); -> END -> // mysql> DELIMITER ;
现在您可以使用CALL命令调用存储过程-
mysql> call insert_proc(89);
使用select语句显示两个表中的所有记录-
mysql> select * from DemoTable1333; +--------------+ | StudentScore | +--------------+ | 89 | +--------------+ 1 row in set (0.00 sec) mysql> select * from DemoTable1334; +-------------+ | PlayerScore | +-------------+ | 89 | +-------------+ 1 row in set (0.00 sec)