您好,欢迎访问一九零五行业门户网

在MySQL中,我们可以使用存储过程同时向两个表中插入记录吗?

是的,您可以使用存储过程在单个查询中插入到两个表中。让我们首先创建一个表 −
mysql> create table demotable( studentid int not null auto_increment primary key, studentfirstname varchar(20));query ok, 0 rows affected (0.56 sec)
这是创建第二个表的查询 -
mysql> create table demotable2( clientid int not null auto_increment primary key, clientname varchar(20), clientage int);query ok, 0 rows affected (0.76 sec)
以下是创建存储过程的查询,以插入到上面创建的两个表中 -
mysql> delimiter // mysql> create procedure insert_into_twotables(name varchar(100),age int) begin insert into demotable(studentfirstname) values(name); insert into demotable2(clientname,clientage) values(name,age); end // query ok, 0 rows affected (0.14 sec)mysql> delimiter ;
现在借助 call 命令调用存储过程 -
mysql> call insert_into_twotables('tom',38);query ok, 1 row affected, 1 warning (0.41 sec)
检查记录是否插入到两个表中。
显示第一个表中所有记录的查询如下 -
mysql> select *from demotable;
这将产生以下输出 -
+-----------+------------------+| studentid | studentfirstname |+-----------+------------------+| 1 | tom |+-----------+------------------+1 row in set (0.00 sec)
以下是显示第二个表中所有记录的查询:
mysql> select *from demotable2;
这将产生以下输出 -
+----------+------------+-----------+| clientid | clientname | clientage |+----------+------------+-----------+| 1 | tom | 38 |+----------+------------+-----------+1 row in set (0.00 sec)
以上就是在mysql中,我们可以使用存储过程同时向两个表中插入记录吗?的详细内容。
其它类似信息

推荐信息