存储过程可以写一条或多条的sql语句
加上
mysql> delimiter \$\$
mysql> create procedure test1()
-> begin
-> select * from db_user;
-> end;
-> \$\$
Query OK, 0 rows affected (0.00 sec)
mysql> call test1()
-> ;
-> $$
1.存储过程的变量
CREATE PROCEDURE `db01`()
BEGIN
#声明变量
DECLARE my_uname varchar(32) default '';
#赋值办法1
set my_uname='hxf';
#赋值办法2
SELECT question into my_uname from db_question where id=4;
SELECT my_uname;
END
call db01
条件语句
CREATE DEFINER=`root`@`localhost` PROCEDURE `db03`(In my_uid int)
BEGIN
DECLARE my_uname varchar(32) default '';
if(my_uid%2=0)
then
select question into my_uname from db_question where id=my_uid;
select my_uname;
elseif(...)
then
......
else
select my_uid;
END if;
END
循环语句
情况一:
CREATE PROCEDURE `db05` ()
BEGIN
DECLARE i INT default 0;
while(i<10) DO
BEGIN
SELECT i;
set i=i+1;
#insert into db_user(name) values(i);
END;
END while;
END;
情况二:
CREATE PROCEDURE `new_procedure` ()
BEGIN
DECLARE i INT default 0;
repeat
BEGIN
SELECT i;
set i=i+1;
#insert into db_user(name) values(i);
END;
until i>10
end repeat;
END
游标
BEGIN
DECLARE stopflag int(11) default 0;
DECLARE my_uname varchar(32) default '';
#游标是一个查询的结果集
DECLARE uname_cur cursor for select name from db_user where id%2=0;
#CONTINUE 是否继续的句柄 CONTINUE handler 跟踪上一个游标的变量
DECLARE CONTINUE handler for NOT found SET stopflag=1;
#打开游标
open uname_cur;
#使用游标
fetch uname_cur INTO my_uname;
while(stopflag=0) DO
begin
#select my_uname;
update db_user set name=concat(my_uname,'_cur') where name=my_uname;
FETCH uname_cur into my_uname;
end;
end while;
#使用完游标要关闭、
close uname_cur;
END
触发器
#创建一个触发器 在添加db_user之后
create trigger tr_user_insert after INSERT on db_user
#对每一行受影响
for each ROW
BEGIN
#添加完一条之后就立刻执行一条sql
INSERT INTO db_test(name,Face)
values(NEW.name,NEW.img);
END
加上
mysql> delimiter \$\$
mysql> create procedure test1()
-> begin
-> select * from db_user;
-> end;
-> \$\$
Query OK, 0 rows affected (0.00 sec)
mysql> call test1()
-> ;
-> $$
1.存储过程的变量
CREATE PROCEDURE `db01`()
BEGIN
#声明变量
DECLARE my_uname varchar(32) default '';
#赋值办法1
set my_uname='hxf';
#赋值办法2
SELECT question into my_uname from db_question where id=4;
SELECT my_uname;
END
call db01
条件语句
CREATE DEFINER=`root`@`localhost` PROCEDURE `db03`(In my_uid int)
BEGIN
DECLARE my_uname varchar(32) default '';
if(my_uid%2=0)
then
select question into my_uname from db_question where id=my_uid;
select my_uname;
elseif(...)
then
......
else
select my_uid;
END if;
END
循环语句
情况一:
CREATE PROCEDURE `db05` ()
BEGIN
DECLARE i INT default 0;
while(i<10) DO
BEGIN
SELECT i;
set i=i+1;
#insert into db_user(name) values(i);
END;
END while;
END;
情况二:
CREATE PROCEDURE `new_procedure` ()
BEGIN
DECLARE i INT default 0;
repeat
BEGIN
SELECT i;
set i=i+1;
#insert into db_user(name) values(i);
END;
until i>10
end repeat;
END
游标
BEGIN
DECLARE stopflag int(11) default 0;
DECLARE my_uname varchar(32) default '';
#游标是一个查询的结果集
DECLARE uname_cur cursor for select name from db_user where id%2=0;
#CONTINUE 是否继续的句柄 CONTINUE handler 跟踪上一个游标的变量
DECLARE CONTINUE handler for NOT found SET stopflag=1;
#打开游标
open uname_cur;
#使用游标
fetch uname_cur INTO my_uname;
while(stopflag=0) DO
begin
#select my_uname;
update db_user set name=concat(my_uname,'_cur') where name=my_uname;
FETCH uname_cur into my_uname;
end;
end while;
#使用完游标要关闭、
close uname_cur;
END
触发器
#创建一个触发器 在添加db_user之后
create trigger tr_user_insert after INSERT on db_user
#对每一行受影响
for each ROW
BEGIN
#添加完一条之后就立刻执行一条sql
INSERT INTO db_test(name,Face)
values(NEW.name,NEW.img);
END