Mysql 触发器和存储过程

ゞ 浴缸里的玫瑰 2022-04-15 02:07 427阅读 0赞

1、首先建表:
create table tababin(
id int not null auto_increment,
name varchar(100),
constraint pk primary key(id)
)

2、拷贝一张相同的表:
create table tababin1 like tababin;

3.建立主键自增触发器:
create trigger triabin before insert on tababin for each ROW
begin
set @new=new.id;
end

4、插入记录:
insert into tababin (name) values (‘abin1’)
insert into tababin (name) values (‘abin2’)
insert into tababin (name) values (‘abin3’)

5‘编写存储过程(带游标和LOOP循环的存储过程):
CREATE PROCEDURE pabin()
begin
declare id,status int ;
declare name varchar(100);
declare mycur cursor for select * from tababin;
declare continue handler for not found set status=1;
open mycur;
set status=0;
loopLabel:loop
fetch mycur into id,name;
if status=0 then
if id is not null then
if name is not null then
insert into tababin1 values (id,name);
end if;
end if;
end if;
if status =1 then
leave loopLabel;
end if;
end loop;
close mycur;
end

6、测试存储过程:
call pabin()

结果:tababin1表里面新增了数据。

发表评论

表情:
评论列表 (有 0 条评论,427人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mysql触发器 存储过程

    一、触发器 MYSQL包含对触发器的支持,触发器是一种与表操作有关的数据库对象,当触发器在表上出现指定事件时,调用该对象,也就是说表的操作事件触发表上的触发器执行。 创