在 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
-> );
Query OK, 0 rows affected (0.58 sec)下面是第二个表:
mysql> create table DemoTable2
-> (
-> PlayerScore int
-> );
Query OK, 0 rows affected (0.52 sec)以下是创建存储过程并在两个表中插入值的查询:
mysql> DELIMITER //
mysql> CREATE PROCEDURE insert_proc(value int )
-> BEGIN
-> insert into DemoTable1(StudentScore) values(value);
-> insert into DemoTable2(PlayerScore) values(value);
-> END
-> //
Query OK, 0 rows affected (0.16 sec)
mysql> DELIMITER ;现在您可以使用 CALL 命令调用存储过程:
mysql> call insert_proc(89);
Query OK, 1 row affected (0.29 sec)
使用 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)